无法使用CNN模型获取输出

我尝试在这个数据集上使用cnn-lstm模型。我已经将这个数据集存储在名为df的数据框中。这个数据集总共有11列,但我在这里只提到了9列。所有列都只有数值数据

Area     book_hotel  votes  location    hotel_type  Total_Price   Facilities   Dine      rate6             0        0      1           163          400             22        7        4.419            1        2      7           122          220             28        11       4.6X=df.drop(['rate'],axis=1) Y=df['rate']x_train, x_test, y_train, y_test = train_test_split(np.asarray(X), np.asarray(Y), test_size=0.33, shuffle= True)

x_train的形状为(3350,10),x_test的形状为(1650, 10)

# 已知输出类别的数量num_classes = 10# 输入图像的尺寸input_shape = (10,)# 将类别向量转换为二进制类别矩阵。这使用了1热编码。y_train_binary = keras.utils.to_categorical(y_train, num_classes)y_test_binary = keras.utils.to_categorical(y_test, num_classes)x_train = x_train.reshape(3350, 10,1)x_test = x_test.reshape(1650, 10,1)input_layer = Input(shape=(10, 1))conv1 = Conv1D(filters=32,               kernel_size=8,               strides=1,               activation='relu',               padding='same')(input_layer)lstm1 = LSTM(32, return_sequences=True)(conv1)output_layer = Dense(1, activation='sigmoid')(lstm1)model = Model(inputs=input_layer, outputs=output_layer)model.summary()model.compile(loss='mse',optimizer='adam')

最后,当我尝试用输入来拟合模型时

 model.fit(x_train,y_train)ValueError                                Traceback (most recent call last)<ipython-input-170-4719cf73997a> in <module>()----> 1 model.fit(x_train,y_train)2 frames/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)    133                         ': expected ' + names[i] + ' to have ' +    134                         str(len(shape)) + ' dimensions, but got array '--> 135                         'with shape ' + str(data_shape))    136                 if not check_batch_axis:    137                     data_shape = data_shape[1:]ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (3350, 1)

有人可以帮助我解决这个错误吗


回答:

我看到你的代码中有一些问题…

最后一维的输出必须等于类别的数量,并且在多类任务中,你需要应用softmax激活函数:Dense(num_classes, activation='softmax')

在你的最后一个LSTM单元中,你必须设置return_sequences=False,因为你需要一个2D输出而不是3D

你必须使用categorical_crossentropy作为损失函数,并使用one-hot编码的目标

这里是一个完整的示例代码…

num_classes = 10n_sample = 1000X = np.random.uniform(0,1, (n_sample,10,1))y = tf.keras.utils.to_categorical(np.random.randint(0,num_classes, n_sample))input_layer = Input(shape=(10, 1))conv1 = Conv1D(filters=32,               kernel_size=8,               strides=1,               activation='relu',               padding='same')(input_layer)lstm1 = LSTM(32, return_sequences=False)(conv1)output_layer = Dense(num_classes, activation='softmax')(lstm1)model = Model(inputs=input_layer, outputs=output_layer)model.compile(loss='categorical_crossentropy',optimizer='adam')model.fit(X,y, epochs=5)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注