简体   繁体   English

我怎样才能用这个 keras 匹配一个数组 model

[英]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.

相关问题 重塑数组以适应 Keras 模型 - Reshaping array to fit in Keras model 我可以在 keras 中多次调用 model.fit 吗? - Can i call model.fit multiple times in keras? 如何在不使用 model.fit_generator 的情况下对 Keras 中的图像应用旋转? - How can I apply rotation to image in Keras without using model.fit_generator? 如何在运行时生成的我自己的数据适合我的keras模型 - How can I fit my keras model with my own data generated at runtime 如何在 keras 中拟合两个串联 LSTM 的模型? - How do I fit the model of two concatenate LSTM in keras? 我如何将我的 dataframe 放入 keras model 中? - How do i fit my dataframe in a keras model? Keras:model.fit()和model.fit_generator()返回历史对象。 如何获得Keras模型? - Keras: model.fit() and model.fit_generator() return history objects. How do I get Keras models? 在 Keras.model.fit 中输入什么作为 Y 以及如何将不同尺寸的数据批次拟合到 Keras Z20F35E630DAF494DFAC 中? - What to input in Keras.model.fit as Y and how do I fit different dimensions batches of data to Keras model? 如何将二进制Keras模型拟合到多类模型中? - How can I fit binary keras models into multiclass models? Keras:我可以使用model.predict而不使用model.predict_generator来预测是否使用model.fit_generator训练模型 - Keras: can I use model.predict but not model.predict_generator to predict if I train the model with model.fit_generator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM