当我通过声明性方法而不是功能性方法定义模型时,Keras将给出不同的结果。这两个模型看起来是等价的,但是使用“ .add()”语法可以工作,而使用声明性语法会产生错误-每次都是不同的错误,但通常是这样的:
A target array with shape (10,1) was passed for an output of shape (None,16) while using as loss `mean_squared_error`. This loss expects targets to have the same shape as the output.
输入形状的自动转换似乎正在发生某些事情,但是我不知道是什么。有人知道我在做什么错吗?为什么这两个模型不完全对等?
import tensorflow as tf
import tensorflow.keras
import numpy as np
x = np.arange(10).reshape((-1,1,1))
y = np.arange(10)
#This model works fine
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(32,input_shape=(1,1),return_sequences = True))
model.add(tf.keras.layers.LSTM(16))
model.add(tf.keras.layers.Dense(1))
model.add(tf.keras.layers.activation('linear'))
#This model fails. But shouldn't this be equivalent to the above?
model2 = tf.keras.Sequential(
{
tf.keras.layers.LSTM(32,return_sequences = True),tf.keras.layers.LSTM(16),tf.keras.layers.Dense(1),tf.keras.layers.activation('linear')
})
#This works
model.compile(loss='mean_squared_error',optimizer='adagrad')
model.fit(x,y,epochs=1,batch_size=1,verbose=2)
#But this doesn't! Why not? The error is different each time,but usually
#something about the input size being wrong
model2.compile(loss='mean_squared_error',optimizer='adagrad')
model2.fit(x,verbose=2)
这两个模型为什么不等效?为什么其中一个可以正确处理输入大小,而另一个却不能呢?第二个模型每次都会失败,并会出现不同的错误(有时甚至可以正常工作),所以我认为也许与第一个模型有一些互动?但是我尝试注释掉第一个模型,但这没有帮助。那么第二个为什么不起作用?
更新:这是第一个和第二个模型的“ model.summary()。它们看起来确实有所不同,但是我不明白为什么。
对于model.summary():
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None,32) 4352
_________________________________________________________________
lstm_1 (LSTM) (None,16) 3136
_________________________________________________________________
dense (Dense) (None,1) 17
_________________________________________________________________
activation (activation) (None,1) 0
=================================================================
Total params: 7,505
Trainable params: 7,505
Non-trainable params: 0
对于model2.summary():
model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_2 (LSTM) (None,32) 4352
_________________________________________________________________
activation_1 (activation) (None,32) 0
_________________________________________________________________
lstm_3 (LSTM) (None,16) 3136
_________________________________________________________________
dense_1 (Dense) (None,1) 17
=================================================================
Total params: 7,505
Non-trainable params: 0```