问题描述
我有一个数据集X,该数据集X由 N = 4000个样本组成,每个样本由 d = 2个特征(连续值)组成,回溯 t = 10次步骤.在时间步骤11,我还具有每个样本的相应标签",它们也是连续值.
I have a dataset X which consists N = 4000 samples, each sample consists of d = 2 features (continuous values) spanning back t = 10 time steps. I also have the corresponding 'labels' of each sample which are also continuous values, at time step 11.
此刻,我的数据集的形状为X:[4000,20],Y:[4000].
At the moment my dataset is in the shape X: [4000,20], Y: [4000].
考虑到先前d个功能的10个输入,我想使用TensorFlow训练LSTM来预测Y值(回归),但是我在TensorFlow中实现这一点非常困难.
I want to train an LSTM using TensorFlow to predict the value of Y (regression), given the 10 previous inputs of d features, but I am having a tough time implementing this in TensorFlow.
我目前遇到的主要问题是了解TensorFlow如何期望输入被格式化.我看过各种示例,例如此,但是这些示例处理了一大串连续时间序列数据.我的数据是不同的样本,每个样本都是一个独立的时间序列.
The main problem I have at the moment is understanding how TensorFlow is expecting the input to be formatted. I have seen various examples such as this, but these examples deal with one big string of continuous time series data. My data is different samples, each an independent time series.
推荐答案
tf.nn.dynamic_rnn
状态的文档:
The documentation of tf.nn.dynamic_rnn
states:
在您的情况下,这意味着输入的形状应为[batch_size, 10, 2]
.不必一次训练所有4000个序列,而是在每个训练迭代中仅使用batch_size
许多序列.像下面这样的东西应该起作用(为清楚起见添加了重塑):
In your case, this means that the input should have a shape of [batch_size, 10, 2]
. Instead of training on all 4000 sequences at once, you'd use only batch_size
many of them in each training iteration. Something like the following should work (added reshape for clarity):
batch_size = 32
# batch_size sequences of length 10 with 2 values for each timestep
input = get_batch(X, batch_size).reshape([batch_size, 10, 2])
# Create LSTM cell with state size 256. Could also use GRUCell, ...
# Note: state_is_tuple=False is deprecated;
# the option might be completely removed in the future
cell = tf.nn.rnn_cell.LSTMCell(256, state_is_tuple=True)
outputs, state = tf.nn.dynamic_rnn(cell,
input,
sequence_length=[10]*batch_size,
dtype=tf.float32)
从文档,的形状将为[batch_size, 10, 256]
,即每个时间步长为256个输出. state
将是元组形状为[batch_size, 256]
的a>.您可以据此预测最终值(每个序列一个):
From the documentation, outputs
will be of shape [batch_size, 10, 256]
, i.e. one 256-output for each timestep. state
will be a tuple of shapes [batch_size, 256]
. You could predict your final value, one for each sequence, from that:
predictions = tf.contrib.layers.fully_connected(state.h,
num_outputs=1,
activation_fn=None)
loss = get_loss(get_batch(Y).reshape([batch_size, 1]), predictions)
outputs
和state
形状的数字256由cell.output_size
确定. cell.state_size
.当像上面创建LSTMCell
时,它们是相同的.另请参见 LSTMCell文档.
The number 256 in the shapes of outputs
and state
is determined by cell.output_size
resp. cell.state_size
. When creating the LSTMCell
like above, these are the same. Also see the LSTMCell documentation.
这篇关于了解Tensorflow LSTM输入形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!