【问题标题】:shapes (335476,50) and (3,50) not aligned: 50 (dim 1) != 3 (dim 0)形状 (335476,50) 和 (3,50) 未对齐:50 (dim 1) != 3 (dim 0)
【发布时间】:2022-01-16 16:54:24
【问题描述】:

我的RNN如下图

length_of_sequence = 3
in_out_neurons = 50
n_hidden = 128
model = Sequential()
model.add(LSTM(n_hidden, batch_input_shape=(None, length_of_sequence,in_out_neurons), return_sequences=True))
model.add(Dense(in_out_neurons,activation="linear"))
optimizer = Adam(lr=0.001)
model.compile(loss="mean_squared_error", optimizer=optimizer)
model.summary()

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 3, 128)            91648     
                                                                 
 dense (Dense)               (None, 3, 50)             6450      
                                                                 
=================================================================
Total params: 98,098
Trainable params: 98,098
Non-trainable params: 0
_________________________________________________________________

然后尝试训练和预测

print(final_x.shape) #(165737, 3, 50)
print(final_y.shape) #(165737, 1, 50)
model.fit(         
    final_x,final_y,
    batch_size=300,
    epochs=10,
    validation_split=0.9
)
print(test_input.shape) # (1, 3, 50)
predicted = model.predict(test_input)

显示错误ValueError: shapes (335476,50) and (3,50) not aligned: 50 (dim 1) != 3 (dim 0)

我不确定 335476 来自哪里......

我应该在哪里修复??

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    您通常使用与训练原始模型相同的batch_size。有关此主题的更多信息和可能的解决方法,请访问 here。但是,由于您使用的是None,它应该适用于单个样本。这是一个工作示例:

    import tensorflow as tf
    
    length_of_sequence = 3
    in_out_neurons = 50
    n_hidden = 128
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.LSTM(n_hidden, batch_input_shape=(None, length_of_sequence,in_out_neurons), return_sequences=True))
    model.add(tf.keras.layers.Dense(in_out_neurons,activation="linear"))
    optimizer = tf.keras.optimizers.Adam(lr=0.001)
    model.compile(loss="mean_squared_error", optimizer=optimizer)
    model.summary()
    
    final_x = tf.random.normal((100, 3, 50))
    final_y = tf.random.normal((100, 3, 50))
    model.fit(         
        final_x,final_y,
        batch_size=2,
        epochs=10,
        validation_split=0.9
    )
    
    test_input = tf.random.normal((1, 3, 50))
    predicted = model.predict(test_input)
    print(predicted.shape)
    
    (1, 3, 50)
    

    【讨论】:

    • 但是,如果您使用 None 作为批量大小,您可以在训练和推理期间使用任意大小的批量。
    • mmm 输入大小必须为(batch_size, 3, 128)?不过,我使用(165737,3,50) 来学习数据......这旨在从 [50dimention]、[50dimention]、[50dimention] 三个连续项目中预测下一个 [50dimention]。
    • 据我了解,在学习过程中,训练数据的形状是 (165737, 3, 50) ,然后使用 (1,3,50) 进行预测。
    • 我在文章里加了model.fit()
    • 我试过你的代码,它可以工作谢谢@AloneTogether 我需要整理形状的想法。
    猜你喜欢
    • 2021-10-12
    • 2020-07-26
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2018-10-05
    • 2019-03-21
    • 2019-05-28
    相关资源
    最近更新 更多