简体   繁体   English

了解Tensorflow LSTM模型输入?

[英]Understanding Tensorflow LSTM models input?

I have some trouble understanding LSTM models in TensorFlow. 我在TensorFlow中理解LSTM模型时遇到一些麻烦。

I use the tflearn as a wrapper, as it does all the initialization and other higher level stuff automatically. 我使用tflearn作为包装器,因为它会自动执行所有初始化和其他更高级别的操作。 For simplicity, let's consider this example program . 为简单起见,让我们考虑一下这个示例程序 Until line 42 , net = tflearn.input_data([None, 200]) , it's pretty clear what happens. 直到第42行net = tflearn.input_data([None, 200]) ,很清楚会发生什么。 You load a dataset into variables and make it of a standard length (in this case, 200). 您将数据集加载到变量中并使其成为标准长度(在本例中为200)。 Both the input variables and also the 2 classes are, in this case, converted to one-hot vectors. 在这种情况下,输入变量和2个类都转换为单热矢量。

How does the LSTM take the input ? LSTM如何接受输入 Across how many samples does it predict the output? 它预测输出的样本数量是多少?

What does net = tflearn.embedding(net, input_dim=20000, output_dim=128) represent? net = tflearn.embedding(net, input_dim=20000, output_dim=128)代表什么?

My goal is to replicate the activity recognition dataset in the paper . 我的目标是在论文中 复制活动识别数据集。 For example, I would like to input a 4096 vector as input to the LSTM, and the idea is to take 16 of such vectors, and then produce the classification result. 例如,我想输入一个4096向量作为LSTM的输入,其思路是采用16个这样的向量,然后产生分类结果。 I think the code would look like this, but I don't know how the input to the LSTM should be given. 我认为代码看起来像这样,但我不知道应该如何给出LSTM的输入。

from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.data_utils import to_categorical, pad_sequences
from tflearn.datasets import imdb

train, val = something.load_data()
trainX, trainY = train #each X sample is a (16,4096) nd float64 
valX, valY = val #each Y is a one hot vector of 101 classes.

net = tflearn.input_data([None, 16,4096])
net = tflearn.embedding(net, input_dim=4096, output_dim=256)
net = tflearn.lstm(net, 256)
net = tflearn.dropout(net, 0.5)
net = tflearn.lstm(net, 256)
net = tflearn.dropout(net, 0.5)
net = tflearn.fully_connected(net, 101, activation='softmax')
net = tflearn.regression(net, optimizer='adam',
                         loss='categorical_crossentropy')

model = tflearn.DNN(net, clip_gradients=0., tensorboard_verbose=3)
model.fit(trainX, trainY, validation_set=(testX, testY), show_metric=True,
          batch_size=128,n_epoch=2,snapshot_epoch=True)

Basically, lstm takes the size of your vector for once cell: 基本上,lstm采用一次单元格的向量大小:

lstm = rnn_cell.BasicLSTMCell(lstm_size, forget_bias=1.0)

Then, how many time series do you want to feed? 那么,您想要提供多少个时间序列? It's up to your fed vector. 这取决于你的喂食矢量。 The number of arrays in the X_split decides the number of time steps: X_split的数组数决定了时间步数:

X_split = tf.split(0, time_step_size, X)
outputs, states = rnn.rnn(lstm, X_split, initial_state=init_state)

In your example, I guess the lstm_size is 256, since it's the vector size of one word. 在你的例子中,我猜lstm_size是256,因为它是一个单词的向量大小。 The time_step_size would be the max word count in your training/test sentences. time_step_size将是训练/测试句中的最大字数。

Please see this example: https://github.com/nlintz/TensorFlow-Tutorials/blob/master/07_lstm.py 请看这个例子: https//github.com/nlintz/TensorFlow-Tutorials/blob/master/07_lstm.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM