输入形状 Keras RNN

Posted

技术标签:

【中文标题】输入形状 Keras RNN【英文标题】:Input Shape Keras RNN 【发布时间】:2020-06-10 03:39:20 【问题描述】:

我正在使用时间序列数据,其形状为 2000x1001,其中 2000 是案例数,1000 行表示时域中的数据,期间 X 方向的位移1 秒周期,表示时间步长为 0.001。最后一列代表速度,即我需要根据 1 秒内的位移预测的输出值。 Keras 中的RNN 应该如何塑造输入数据?我已经学习了一些教程,但我仍然对 RNN 中的输入形状感到困惑。提前致谢

#load data training data
dataset=loadtxt("Data.csv", delimiter=",")
x = dataset[:,:1000]
y = dataset[:,1000]


#Create train and test dataset with an 80:20 split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2) 

#input scaling
scaler = StandardScaler()
x_train_s =scaler.fit_transform(x_train)
x_test_s = scaler.transform(x_test)

num_samples = x_train_s.shape[0] ## Number of samples
num_vals    = x_train_s.shape[1] # Number of elements in each sample

x_train_s = np.reshape(x_train_s, (num_samples, num_vals, 1))

#create model
model = Sequential()
model.add(LSTM(100, input_shape=(num_vals, 1)))
model.add(Dense(1, activation='relu'))
model.compile(loss='mae', optimizer='adam',metrics = ['mape'])
model.summary()

#training
history = model.fit(x_train_s, y_train,epochs=10, verbose = 1, batch_size =64)

【问题讨论】:

嗨安德鲁,我已经添加了一个答案,请阅读它,它肯定有助于理解论点。 【参考方案1】:

看看这段代码: 它试图根据前 6 个值预测下 4 个值。 跟随 cmets 并查看如何操作非常简单的输入以使用它 作为 rnn/lstm 中的输入

遵循代码中的 cmets

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow.keras import Model
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import RNN, LSTM

"""
creating a toy dataset
lets use this below ```input_sequence``` as the sequence to make data points.
as per the question, we will use 6 points to predict next 4 points
"""
input_sequence = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]

X_train = []
y_train = []


**#first 6 points will be our input data points and next 4 points will be data label.
#so on we will shift by 1 and make such data points and label pairs**


for i in range(len(input_sequence)-9):
    X_train.append(input_sequence[i:i+6])
    y_train.append(input_sequence[i+6:i+10])

X_train = np.array(X_train, dtype=np.float32)
y_train = np.array(y_train, dtype=np.int32)))


**#X_test for the predictions (contains 6 points)**


X_test = np.array([[8,9,10,1,2,3]],dtype=np.float32)
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)


**#we will be using basic LSTM, which accepts input in ```[num_inputs, time_steps, data_points], therefore reshaping as per that```** 
# so here:
# 1. num_inputs = how many sequence of 6 points you want to use i.e. rows (we use X_train.shape[0] )

# 2. time_steps = batches you can considered i.e. if you want to use 1 or 2 or 3 rows

# 3. data_points = number of points (for ex. in our case its 6 points we are using)

X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)

x_points = X_train.shape[-1]
print("one input contains  points".format(x_points))

model = Sequential()
model.add(LSTM(4, input_shape=(1, x_points)))
model.add(Dense(4))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()

model.fit(X_train, y_train, epochs=500, batch_size=5, verbose=2)
output = list(map(np.ceil, model.predict(X_test)))
print(output)

希望对您有所帮助。请提出任何疑问。

【讨论】:

您好,我已在说明中提供了我的脚本。看来我的问题还不够清楚。我有一个数据集,其中包含 2000 个样本、1000 个时域位移和位于第 1001 列的相应速度。我的任务是根据位移对速度进行预测。我的问题是设置 RNN 所需的输入形状。 代码中的x_train_s = np.reshape(x_train_s, (num_samples, num_vals, 1)),将其重写为:x_train_s = np.reshape(x_train_s, (num_samples, 1, num_vals))。因为格式应该是(num_samples,timestep,num_vals)。请尝试一次。 上述方法你试过了吗? 有效,我也把model.add(LSTM(100, input_shape=(num_vals, 1)))改成了model.add(LSTM(100, input_shape=(num_vals, 1))),但还是不清楚。您提到格式是(num_samples,timestep,num_vals),在我的情况下 num_samples = 2000,num_vals 是一个 timestep 并且等于 1000,最后特征数是 1,它指的是我试图预测的 Y。那么,你为什么提出 (2000, 1, 1000) 而不是 (2000, 1000, 1)? link我用这个例子作为参考【参考方案2】:

就像doc 中解释的那样,Keras 期望 RNN 具有以下形状:

(batch_size, timesteps, input_dim)
batch_size 是您在反向传播之前提供的样本数 timesteps 是每个样本的时间步数 input_dim 是每个时间步的特征数

编辑更多细节:

在你的情况下,你应该去

batch_input_shape = (batch_size, timesteps, 1)

根据需要选择batch_sizetimesteps

时间步长呢?

假设您取了 2000 个样本中的一个,假设您的样本有 10 个元素而不是 1000 个,例如:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

那么,如果我们选择timesteps=3,那么你会得到一个长度为8的批次:

[[[0], [1], [2]],
 [[1], [2], [3]],
 [[2], [3], [4]],
 [[3], [4], [5]],
 [[4], [5], [6]],
 [[5], [6], [7]],
 [[6], [7], [8]],
 [[7], [8], [9]]]

【讨论】:

这是否意味着在我的情况下输入的形状为 (2000,1000,1)

以上是关于输入形状 Keras RNN的主要内容,如果未能解决你的问题,请参考以下文章

Recurrentshop 和 Keras:多维 RNN 导致维度不匹配错误

Keras RNN输入维度问题

如何有条件地缩放 Keras Lambda 层中的值?

如何修复'ValueError:输入0与层simple_rnn_1不兼容:预期形状=(无,无,20),找到形状=(无,无,2,20)'

使用Keras与LSTM和RNN进行斗争

带有 LSTM 单元的 Keras RNN 用于基于多个输入时间序列预测多个输出时间序列