简体   繁体   English

Python:无法在Keras中训练回归模型

[英]Python: cannot train regression model in Keras

I am trying to train a DNN with Keras. 我正在尝试与Keras一起训练DNN。 The model is here defined: 在此定义模型:

model = Sequential()
model.add(Dense(2050, input_shape=(2050, 75), activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(75, activation='sigmoid'))

The cost function is the mse. 成本函数是毫秒。 The idea here is training with a set of 3000 images with size 2050*75, which basically are two different features extracted from a 1025*75 image, in order to get 3000 image with size 1025*75 in the output, which are some kind of representation of the original image. 这里的想法是训练一组3000张尺寸为2050 * 75的图像,这基本上是从1025 * 75图像中提取的两个不同特征,以便在输出中获得3000张尺寸为1025 * 75的图像,这是某种原始图像的表示形式。

So, the input is a (3000, 2050, 75) tensor, while the output dimension is (3000, 1025, 75). 因此,输入为(3000,2050,75)张量,而输出尺寸为(3000,1025,75)。

I can see why Keras gives me the following error: 我可以看到Keras为什么给我以下错误:

ValueError: Error when checking target: expected dense_5 to have shape (None, 2050, 75) but got array with shape (3000, 1025, 75) ValueError:检查目标时出错:预期density_5具有形状(None,2050,75),但数组的形状为(3000,1025,75)

There must be a way to avoid this error, maybe by redefining the DNN dimensions or layers. 必须有一种避免此错误的方法,可能是通过重新定义DNN尺寸或图层。 Do you have any suggestion? 你有什么建议吗? Thanks. 谢谢。

EDIT: As requested, this is the complete code. 编辑:根据要求,这是完整的代码。

X = train_set
Y = m
[n_samples, n_freq, n_time] = X.shape

model = Sequential()
model.add(Dense(n_freq, input_shape=(n_freq, n_time), activation='relu'))
model.add(Dense(n_hid, activation='relu'))
model.add(Dense(n_hid, activation='relu'))
model.add(Dense(n_hid, activation='relu'))
model.add(Dense(n_time, activation='sigmoid'))

model.summary()
model.compile(optimizer='rmsprop',loss='mse',metrics=['mae','accuracy'])
model.fit(np.abs(X), np.abs(Y), epochs=n_epochs, batch_size=batch_size)
score = model.evaluate(np.abs(X), np.abs(Y), batch_size = batch_size)

Since, you cannot reshape the array internally using the reshape layer, because total size of new array must be unchanged. 因为您不能在内部使用重塑层重塑阵列,因为新阵列的总大小必须保持不变。 I suggest to flat the tensor, using the flatten layer. 我建议使用平整层平整张量。 But first, you need to reshape the y: 但首先,您需要重塑y:

y = y.reshape(-1, 1025*75)

the updated model would look like this: 更新后的模型如下所示:

model = Sequential()
model.add(Dense(n_freq, input_shape=(n_freq, n_time), activation='relu'))
model.add(Dense(n_hid, activation='relu'))
model.add(Dense(n_hid, activation='relu'))
model.add(Dense(n_hid, activation='relu'))
model.add(Flatten())
model.add(Dense(1025*75, activation='sigmoid'))

model.summary()
model.compile(optimizer='rmsprop',loss='mse',metrics=['mae','accuracy'])
model.fit(np.abs(X), np.abs(y), epochs=n_epochs, batch_size=batch_size)
#score = model.evaluate(np.abs(X), np.abs(Y), batch_size = batch_size)

After, you can reshape you y_pred back to (-1,1015,75) shape: 之后,您可以将y_pred重塑为(-1,1015,75)形状:

y_pred = y_pred.reshape(-1,1015,75)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM