如何为LSTM调整2功能输入数据

时间:2019-05-23 18:58:36

标签: arrays machine-learning multidimensional-array keras lstm

我正在Keras中使用带有LSTM节点的RNN进行时间序列预测。我有两个输入功能和一个输出功能,并且我正在使用大小为4,步长为1的滑动窗口。

因此,我正在尝试相应地准备数组,以供LSTM处理数据。但是,尺寸似乎不正确。我已经知道要使3D阵列具有适合网络使用的正确形状,但是在我看来,在阵列中设置数据的方式似乎不合适。

因此,仅查看培训数据,这是文件中的原始数据:

train_input = df[['a','b']].values (this is of shape (354, 2))
train_output = df[['c']].values (this is of shape (354, 1))

接下来,我缩放数据,此后形状仍然保持不变。然后我想使用一个循环,以使数据进入滑动窗口形状(窗口大小4,范围354):

train_input_window = []
train_output_window = []
for i in range(4, 354):
   train_input_window.append(train_input_scaled[i-4:i, 0])   
   train_input_window.append(train_input_scaled[i-4:i, 1])  
   train_output_window.append(train_output_scaled[i, 0])
train_input_window = np.array(train_input_window)
train_output_window = np.array(train_output_window)

现在train_input_window的形状为(700,4) 并且train_output_window的形状为(350,)

我想这就是问题所在。因为我可以将数据重塑为可以正常工作的3D数组:

train_input3D = np.reshape(train_input_window, (350,4,2))
train_output3D = np.reshape(train_output_window, (350,1,1))

但是我只是不认为数据在数组内的排列正确。 训练输入看起来像这样:

print(train_input3D)
[[[a a]
  [a a]
  [b b]
  [b b]]

 [[a a]
  [a a]
  [b b]
  [b b]].....

应该不是

[[[a b]
  [a b]
  [a b]
  [a b]]

 [[a b]
  [a b]
  [a b]
  [a b]].....

我尝试了很多不同的东西,但现在我很困惑,我只是希望我也不要通过尝试解释来混淆这里的所有人。

那么,我认为我的数组应该与Im尝试的输入形状正确吗?如果是这样,我该如何安排呢? 这是我完整的代码:

#Read Data
df = pd.ExcelFile('GPT.xlsx').parse('7Avg')

# Training Data
train_input = df[['Precip_7Sum','Temp_7Avg']].values#
train_output = df[['GWL_7Avg']].values

# Testing Data
test_input = df[['Precip_7SumT','Temp_7AvgT']].values#
test_output = df[['GWL_7AvgT']].values

# normalize / scale Data
input_scaler = MinMaxScaler(feature_range = (0, 1))
output_scaler = MinMaxScaler(feature_range = (0, 1))

train_input_scaled = input_scaler.fit_transform(train_input) 
train_output_scaled = output_scaler.fit_transform(train_output)
test_input_scaled = input_scaler.transform(test_input)
test_output_scaled = output_scaler.transform(test_output)

# Convert Data into sliding window format
train_input_window = [] 
train_output_window = []  
for i in range(4, 354):      
   train_input_window.append(train_input_scaled[i-4:i, 0])   
   train_input_window.append(train_input_scaled[i-4:i, 1])  
   train_output_window.append(train_output_scaled[i, 0])
train_input_window = np.array(train_input_window)
train_output_window = np.array(train_output_window)

test_input_window = []  
test_output_window = []  
for i in range(4, 354):  
   test_input_window.append(train_input_scaled[i-4:i, 0])
   test_input_window.append(train_input_scaled[i-4:i, 1])
   test_output_window.append(train_output_scaled[i, 0])
test_input_window = np.array(test_input_window) 
test_output_window = np.array(test_output_window)

# Convert data into 3-D Formats
train_input3D = np.reshape(train_input_window, (350,train_input_window.shape[1],2)) # 3D tensor with shape (batch_size, timesteps, input_dim) // (nr. of samples, nr. of timesteps, nr. of features)
train_output3D = np.reshape(train_output_window, (350,1,1)) # 

test_input3D = np.reshape(test_input_window, (350,test_input_window.shape[1],2))

# Instantiate model class
model = Sequential()

# Add LSTM layer
model.add(LSTM(units=1, return_sequences = True, input_shape = (4,2)))

# Add dropout layer to avoid over-fitting 
model.add(Dropout(0.2))

# add three more LSTM and Dropouts
model.add(LSTM(units=1, return_sequences=True))  
model.add(Dropout(0.2))

model.add(LSTM(units=1, return_sequences=True))  
model.add(Dropout(0.2))

model.add(LSTM(units=1, return_sequences=True))  
model.add(Dropout(0.2))

# Create dense layer at the end of the model to make model more robust
model.add(Dense(units = 1, output_shape = (4,1)))

# Compile model
model.compile(optimizer = 'adam', loss = 'mean_squared_error')

# Training
model.fit(train_input3D, train_output_window, epochs = 100, batch_size = 4)

# Testing / predictions
train_predictions = model.predict(train_input3D)
test_predictions = model.predict(test_input3D)

# Reverse scaling of data for output data
train_predictions = input_scaler.inverse_transform(train_predictions)
test_predictions = input_scaler.inverse_transform(test_predictions)
orig_data = train_output.append(test_output)

在此方面的所有帮助将不胜感激。我希望我能清楚地解决我的问题,并希望有人可以帮助您阅读所有:D

0 个答案:

没有答案