machine-learning - 无法使用 CNN 模型获得输出
问题描述
我正在尝试在此数据集上使用 cnn-lstm 模型。我已将此数据集存储在名为 df 的数据框中。这个数据集中共有 11 列,但我在这里只提到 9 列。所有列都只有数值
Area book_hotel votes location hotel_type Total_Price Facilities Dine rate
6 0 0 1 163 400 22 7 4.4
19 1 2 7 122 220 28 11 4.6
X=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)
# The known number of output classes.
num_classes = 10
# Input image dimensions
input_shape = (10,)
# Convert class vectors to binary class matrices. This uses 1 hot encoding.
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')
您必须return_sequences=False
在最后一个 lstm 单元格中设置,因为您需要 2D 输出而不是 3D
您必须使用categorical_crossentropy
one-hot 编码目标作为损失函数
这是一个完整的虚拟示例...
num_classes = 10
n_sample = 1000
X = 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)
推荐阅读
- javascript - es6 使用父 id 生成根节点的路径
- ios - 为什么 UIButton.showsTouchWhenHighlighted 不需要将 UIScrollView.delaysContentTouches 设置为 false?
- javascript - Angular:从formArray获取数据并将其发送到api
- python - Python - 使用临时安全凭证创建 AWS 签名
- flutter - StateNotifierProvider 在应用程序重新启动之间不保持状态
- angular - 如何使用 ngrx/component-store 示例在初始化时不发出事件?
- java - 如何简单地制作多个 javafx 事件处理程序?
- system.text.json - System.Text.Json 反序列化并获取属性值
- android - 如何将此字符串拆分回列表?使用科特林
- reactjs - React-Redux 条件变量