Python 输入形状Keras RNN
我使用的是一个时间序列数据,其形状为2000x1001,其中2000是案例数,1000行代表时域数据,1秒期间X方向的位移,这意味着时间步长为0.001。最后一列表示速度,我需要根据1秒内的位移预测的输出值。在Keras中,应如何为RNN塑造输入数据?我已经阅读了一些教程,但仍然对RNN中的输入形状感兴趣。提前谢谢Python 输入形状Keras RNN,python,keras,recurrent-neural-network,Python,Keras,Recurrent Neural Network,我使用的是一个时间序列数据,其形状为2000x1001,其中2000是案例数,1000行代表时域数据,1秒期间X方向的位移,这意味着时间步长为0.001。最后一列表示速度,我需要根据1秒内的位移预测的输出值。在Keras中,应如何为RNN塑造输入数据?我已经阅读了一些教程,但仍然对RNN中的输入形状感兴趣。提前谢谢 #加载数据训练数据 dataset=loadtxt(“Data.csv”,delimiter=“,”) x=数据集[:,:1000] y=数据集[:,1000] #以80:20的比例
#加载数据训练数据
dataset=loadtxt(“Data.csv”,delimiter=“,”)
x=数据集[:,:1000]
y=数据集[:,1000]
#以80:20的比例创建训练和测试数据集
x_系列,x_测试,y_系列,y_测试=系列测试分割(x,y,测试尺寸=0.2)
#输入缩放
scaler=StandardScaler()
x_列\u s=缩放器。拟合变换(x_列)
x_-test_-s=缩放器转换(x_-test)
num samples=x\u train_s.shape[0]#样本数
num_vals=x_train_s.shape[1]#每个样本中的元素数量
x_序列=np.重塑(x_序列,(样本数,数值,1))
#创建模型
模型=顺序()
model.add(LSTM(100,input_shape=(num_vals,1)))
model.add(密集(1,activation='relu'))
compile(loss='mae',optimizer='adam',metrics='mape']
model.summary()
#训练
历史=模型.fit(x\u序列,y\u序列,历代=10,详细=1,批量大小=64)
如中所述,Keras希望RNN具有以下形状:
(batch_size, timesteps, input_dim)
是在backprop之前输入的样本数batch_size
是每个样本的时间步数timesteps
是每个时间步的功能数量input\u dim
batch_input_shape = (batch_size, timesteps, 1)
根据需要选择batch\u size
和timesteps
时间步长如何?
假设您从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]]]
请看以下代码:
它试图根据之前的6个值预测接下来的4个值。
按照注释进行操作,看看如何使用非常简单的输入
作为rnn/lstm中的输入
遵循代码中的注释
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)
希望能有帮助。请询问任何疑问。这是否意味着在我的情况下输入的形状为(20001000,1)?嗨,安德鲁,我已经添加了一个答案,请阅读,这肯定有助于理解参数。您好,我在描述中提供了我的脚本。看来我的问题还不够清楚。我有一个数据集,它有2000个样本,1000个时域位移和相应的速度,位于第1001列。我的任务是根据位移预测速度。我的问题是设置RNN所需的输入形状。而不是
x\u train\u s=np.重塑(x\u train\u s,(num\u samples,num\u vals,1))
在代码中,将其重写为:x\u train\u s=np.重塑(x\u train\u s,(num\u samples,1,num\u vals))
。因为格式应该是(num_samples、timestep、num_vals)。请再试一次。你试过上面的方法吗?它有效,我也更改了模型。添加(LSTM(100,输入形状=(num\u vals,1)))
到模型。添加(LSTM(100,输入形状=(num\u vals,1))
,但仍然不清楚。您提到的格式是(num_samples,timestep,num_vals),在我的例子中num_samples=2000,num_vals是一个timestep,等于1000,最后特征数是1,它指的是我试图预测的Y。那么,你为什么要提出(2000,1,1000)而不是(2000,1000,1)?我用这个例子作为参考