Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多步预测的LSTM网络.py--Unrecognized keyword arguments passed to LSTM: {'batch_size': {1,1,1}} #19

Open
yawn1996 opened this issue May 24, 2024 · 0 comments

Comments

@yawn1996
Copy link

yawn1996 commented May 24, 2024

代码修改如下:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM

出现问题的代码块:

def fit_lstm(train, n_lag, n_seq, n_batch, nb_epoch, n_neurons):
    # (train, 历史1lag, 预测3个步长, 用1 batch数据, 15网络次数_权重更新, 1个神经元)
    # 重塑训练数据格式 [samples, timesteps, features]
    X, y = train[:, 0:n_lag], train[:, n_lag:]  # 1列22个历史数据,3列22个未来数据
    X = X.reshape(X.shape[0], 1, X.shape[1]) # (22,1,1)
    # 配置一个LSTM神经网络,添加网络参数
    model = Sequential()
    #1 model.add(LSTM(n_neurons,input_length=X.shape[1], input_dim=X.shape[2])) #TEST: input_length
    #1 return Unrecognized keyword arguments passed to LSTM: {'input_length': 1}
    #2 model.add(LSTM(units=n_neurons,batch_size=n_batch,input_shape=(X.shape[1], X.shape[2]))) #TEST: batch_size
    #2 return Unrecognized keyword arguments passed to LSTM: {'batch_size': 1}
    #3 model.add(LSTM(units=n_neurons,activation='tanh',batch_input_shape=(None, X.shape[1], X.shape[2]),stateful=True)) #TEST: batch_input_shape
    #3 return Unrecognized keyword arguments passed to LSTM: {'batch_input_shape': (1, 1, 1)}
    model.add(LSTM(units=n_neurons,input_shape=(X.shape[1], X.shape[2]))) 
    # return 'Sequential' object has no attribute 'reset_states'
    # stateful = True --> 需要shuffle = False
    model.add(Dense(y.shape[1]))
    model.compile(loss='mean_squared_error', optimizer='adam')
    # 调用网络,迭代数据对神经网络进行训练,最后输出训练好的网络模型
    for i in range(nb_epoch):
        model.fit(X, y, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
        model.reset_states()
    return model

model = fit_lstm(train, n_lag, n_seq, n_batch, n_epochs, n_neurons)

尝试各种不同的add lstm的方法,均失败
安装包版本:
tensorflow 2.16.1
keras 3.2.1
python 3.9.19

@yawn1996 yawn1996 changed the title 多步预测的LSTM网络--Unrecognized keyword arguments passed to LSTM: {'batch_size': {1,1,1}} 多步预测的LSTM网络.py--Unrecognized keyword arguments passed to LSTM: {'batch_size': {1,1,1}} May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant