[英]How can I fit an array in keras with this model
def kerasModel(inp_shape, activation, n):
lstm_input = keras.layers.Input(shape=inp_shape, name='lstm_input')
x = keras.layers.LSTM(50, name='lstm_0')(lstm_input)
x = keras.layers.Dropout(0.2, name='lstm_dropout_0')(x)
x = keras.layers.Dense(64, name='dense_0')(x)
x = keras.layers.Activation('sigmoid', name='sigmoid_0')(x)
x = keras.layers.Dense(n, name='dense_1')(x)
output = keras.layers.Activation(activation, name='linear_output')(x)
model = keras.Model(inputs=lstm_input, outputs=output)
adam = keras.optimizers.Adam(lr=0.0005)
model.compile(optimizer=adam, loss='mse')
return model
modelGeneral = kerasModel((4, 1), 'linear', 1)
modelGeneral.fit(np.reshape(X_aux['X_i'], (1, 4, 1)), np.reshape(X_aux['X_i1'], (1, 4, 1)), verbose=False)
Returns me this error:返回此错误:
>>> modelGeneral.fit(np.reshape(X_aux['X_i'], (1, 4, 1)), np.reshape(X_aux['X_i1'], (1, 1, 4)), verbose=False)
ValueError: Error when checking target: expected linear_output to have 2 dimensions, but got array with shape (1, 1, 4)
>>> modelGeneral.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_input (InputLayer) (None, 4, 1) 0
_________________________________________________________________
lstm_0 (LSTM) (None, 50) 10400
_________________________________________________________________
lstm_dropout_0 (Dropout) (None, 50) 0
_________________________________________________________________
dense_0 (Dense) (None, 64) 3264
_________________________________________________________________
sigmoid_0 (Activation) (None, 64) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 65
_________________________________________________________________
linear_output (Activation) (None, 1) 0
=================================================================
Total params: 13,729
Trainable params: 13,729
Non-trainable params: 0
_________________________________________________________________
I've tried to reshape the data before linear_output
but it returns another error:我试图在linear_output
之前重塑数据,但它返回另一个错误:
>>> x = keras.layers.Reshape(inp_shape)(x)
ValueError: total size of new array must be unchanged
I think that maybe the problem can be found in np.reshape(X_aux['X_i1'], (1, 1, 4))
in Y->fit()
but honestly I'm lost, so I would appreciate a bit of help!!我认为也许可以在Y->fit()
的np.reshape(X_aux['X_i1'], (1, 1, 4))
中找到问题,但老实说我迷路了,所以我会很感激帮助!!
An example of np.reshape(X_aux['X_i1'], (1, 1, 4))
: np.reshape(X_aux['X_i1'], (1, 1, 4))
的一个例子:
array([[[ 1.5357086, 3.84368446, 3.84368446, 232. ]]])
LSTM layer should return sequences: LSTM 层应返回序列:
x = keras.layers.LSTM(50, return_sequences=True, name='lstm_0')(lstm_input)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.