【问题标题】:How can I fit an array in keras with this model如何使用此模型在 keras 中拟合数组
【发布时间】:2021-04-01 06:08:51
【问题描述】:
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)

返回这个错误:

>>> 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
_________________________________________________________________

我尝试在linear_output 之前重塑数据,但它返回另一个错误:

>>> x = keras.layers.Reshape(inp_shape)(x)
ValueError: total size of new array must be unchanged

我认为也许问题可以在Y->fit() 中的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. ]]])

【问题讨论】:

  • 我无法使用您提供的代码重现您的错误。你能提供一个可重现的例子吗?
  • @NicolasGervais 我想我现在解决了!
  • @yoyoyo 发布您是如何解决的,然后您可以关闭问题。
  • @ranka47 不,我的意思是我已经解决了无法重现代码的错误。我的错误仍未修复

标签: python numpy tensorflow machine-learning keras


【解决方案1】:

LSTM 层应该返回序列:

x = keras.layers.LSTM(50, return_sequences=True, name='lstm_0')(lstm_input)

【讨论】:

  • 你的意思是linear_output 输出必须是(1, 1)?我该怎么做?
  • 它返回给我这个:ValueError: Error when checking input: expected lstm_input to have 3 dimensions, but got array with shape (4, 1)
  • @yoyoyo 线性输出形状已经是 (1, 1)。您需要了解 - 您的数据是什么意思,为什么您的输入是 (1, 4, 1) 而标签是 (1, 1, 4)。如果这些形状是正确的 - 那么你的模型是错误的
  • 对不起,这是一个错误。数据目标也是 (1, 4, 1)。它的形状是因为他们需要将它传递给 LSTM 层。
猜你喜欢
  • 2017-05-11
  • 1970-01-01
  • 2022-08-22
  • 2020-05-12
  • 1970-01-01
  • 2023-04-04
  • 2021-08-12
  • 2019-10-20
  • 2021-09-30
相关资源
最近更新 更多