LSTM class, got error: TypeError: __init__() got an unexpected keyword argument 'input_shape'
LSTM class, got error: TypeError: __init__() got an unexpected keyword argument 'input_shape'
class LSTM:
scaler_y_train= MinMaxScaler(feature_range=(0,1))
scaler_x_train= MinMaxScaler(feature_range=(0,1))
scaler_forecast= MinMaxScaler(feature_range=(0,1))
def __init__(self, filename, seq_len, forecast_size):
self._filename= filename
self._seq_len= seq_len
self._forecast_size= forecast_size
self.model= Sequential()
def load_y_train(self):
raw_data_values= self._filename.values
raw_data= self._filename
print('Initializing the data loading...')
forecast_out_variable= math.ceil(len(raw_data_values)* self._forecast_size)
forecast_out_fixed= None
print('Creating y_train...')
y= raw_data.iloc[forecast_out_variable:].values
len_y= len(y)
data_windows_y= []
for i in range(len_y - self._seq_len):
data_windows_y.append(y[i: i + self._seq_len])
data_windows_y= np.array(data_windows_y).astype(float)
y_train= data_windows_y[:, -1, [0]]
return print(y_train.shape), y_train
def load_x_train(self):
raw_data_values= self._filename.values
raw_data= self._filename
print('Initializing the data loading...')
forecast_out_variable= math.ceil(len(raw_data_values)* self._forecast_size)
forecast_out_fixed= None
print('Creating x_train...')
x= raw_data.iloc[:-forecast_out_variable].values
len_x= len(x)
data_windows_x= []
for i in range(len_x - self._seq_len):
data_windows_x.append(x[i: i + self._seq_len])
data_windows_x= np.array(data_windows_x).astype(float)
x_train= data_windows_x[:, :-1]
return x_train
def load_x_forecast(self):
raw_data_values= self._filename.values
raw_data= self._filename
print('Initializing the data loading...')
forecast_out_variable= math.ceil(len(raw_data_values)* self._forecast_size)
forecast_out_fixed= None
print('Creating x_train...')
x_forecast = raw_data.iloc[:-forecast_out_variable].values
len_x_forecast= len(x_forecast)
data_windows_x= []
for i in range(len_x_forecast - self._seq_len):
data_windows_x.append(x_forecast[i: i + self._seq_len])
data_windows_x= np.array(data_windows_x).astype(float)
xx_forecast= data_windows_x[:, :-1]
return print(xx_forecast.shape), xx_forecast
def build_model(self):
x_train= self.load_x_train()
x_train= np.array(x_train).reshape(1050,49,2).astype(float)
print(x_train.shape)
print('Model starting compiling...')
start= time.time()
self.model.add(LSTM(50, input_shape=(x_train.shape[1], x_train.shape[-1]),
return_sequences=True))
self.model.add(Dropout(0.2))
self.model.add(LSTM(100, return_sequences=False))
self.model.add(Dropout(0.2))
self.model.add(Dense(1, activation = "linear"))
self.model.compile(loss='mse', optimizer='adam')
end= time.time()
print ('model compiled in: ' +str((end-start)*1000)+ ' ms')
return model, print(model.summary())
lstm_01= LSTM(ms_unreal, 50, 0.1)
print(lstm_01.load_x_forecast())
print(lstm_01.load_y_train())
print(lstm_01.load_x_train())
print(lstm_01.build_model(model))
类型错误是 init() 得到了一个意外的关键字参数 'input_shape',
我不知道如何让它工作,因为我正在正确调用库。我在网上搜索了这个问题,但我无法理解这个问题。
该代码是一个名为 LSTM 的 class,其中基本上第一个函数已完成以获取训练和测试数据,而最后一个函数(存在问题)是初始化 LSTM keras 模型。
您的 class 称为 LSTM,您从 Keras 调用一个名为 LSTM 的函数。尝试重命名您的 class My_LSTM 或某些变体。否则,您将无法在不覆盖 Keras 实现的情况下调用 class。
class LSTM:
scaler_y_train= MinMaxScaler(feature_range=(0,1))
scaler_x_train= MinMaxScaler(feature_range=(0,1))
scaler_forecast= MinMaxScaler(feature_range=(0,1))
def __init__(self, filename, seq_len, forecast_size):
self._filename= filename
self._seq_len= seq_len
self._forecast_size= forecast_size
self.model= Sequential()
def load_y_train(self):
raw_data_values= self._filename.values
raw_data= self._filename
print('Initializing the data loading...')
forecast_out_variable= math.ceil(len(raw_data_values)* self._forecast_size)
forecast_out_fixed= None
print('Creating y_train...')
y= raw_data.iloc[forecast_out_variable:].values
len_y= len(y)
data_windows_y= []
for i in range(len_y - self._seq_len):
data_windows_y.append(y[i: i + self._seq_len])
data_windows_y= np.array(data_windows_y).astype(float)
y_train= data_windows_y[:, -1, [0]]
return print(y_train.shape), y_train
def load_x_train(self):
raw_data_values= self._filename.values
raw_data= self._filename
print('Initializing the data loading...')
forecast_out_variable= math.ceil(len(raw_data_values)* self._forecast_size)
forecast_out_fixed= None
print('Creating x_train...')
x= raw_data.iloc[:-forecast_out_variable].values
len_x= len(x)
data_windows_x= []
for i in range(len_x - self._seq_len):
data_windows_x.append(x[i: i + self._seq_len])
data_windows_x= np.array(data_windows_x).astype(float)
x_train= data_windows_x[:, :-1]
return x_train
def load_x_forecast(self):
raw_data_values= self._filename.values
raw_data= self._filename
print('Initializing the data loading...')
forecast_out_variable= math.ceil(len(raw_data_values)* self._forecast_size)
forecast_out_fixed= None
print('Creating x_train...')
x_forecast = raw_data.iloc[:-forecast_out_variable].values
len_x_forecast= len(x_forecast)
data_windows_x= []
for i in range(len_x_forecast - self._seq_len):
data_windows_x.append(x_forecast[i: i + self._seq_len])
data_windows_x= np.array(data_windows_x).astype(float)
xx_forecast= data_windows_x[:, :-1]
return print(xx_forecast.shape), xx_forecast
def build_model(self):
x_train= self.load_x_train()
x_train= np.array(x_train).reshape(1050,49,2).astype(float)
print(x_train.shape)
print('Model starting compiling...')
start= time.time()
self.model.add(LSTM(50, input_shape=(x_train.shape[1], x_train.shape[-1]),
return_sequences=True))
self.model.add(Dropout(0.2))
self.model.add(LSTM(100, return_sequences=False))
self.model.add(Dropout(0.2))
self.model.add(Dense(1, activation = "linear"))
self.model.compile(loss='mse', optimizer='adam')
end= time.time()
print ('model compiled in: ' +str((end-start)*1000)+ ' ms')
return model, print(model.summary())
lstm_01= LSTM(ms_unreal, 50, 0.1)
print(lstm_01.load_x_forecast())
print(lstm_01.load_y_train())
print(lstm_01.load_x_train())
print(lstm_01.build_model(model))
类型错误是 init() 得到了一个意外的关键字参数 'input_shape', 我不知道如何让它工作,因为我正在正确调用库。我在网上搜索了这个问题,但我无法理解这个问题。 该代码是一个名为 LSTM 的 class,其中基本上第一个函数已完成以获取训练和测试数据,而最后一个函数(存在问题)是初始化 LSTM keras 模型。
您的 class 称为 LSTM,您从 Keras 调用一个名为 LSTM 的函数。尝试重命名您的 class My_LSTM 或某些变体。否则,您将无法在不覆盖 Keras 实现的情况下调用 class。