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的类,其中基本上第一个函数被最终确定以获得训练和测试数据,而最后一个(存在问题)用于初始化LSTM keras模型。
您的类称为LSTM,您从Keras调用了一个函数,称为LSTM。尝试重命名您的类My_LSTM或某些变体。否则,您将无法在不覆盖Keras实现的情况下调用您的类。