本文介绍了了解Tensorflow LSTM模型输入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解TensorFlow中的LSTM模型时遇到了一些麻烦.

I have some trouble understanding LSTM models in TensorFlow.

我将 tflearn 用作包装器,因为它会自动执行所有初始化和其他更高级别的工作.为简单起见,让我们考虑此示例程序.直到第42行net = tflearn.input_data([None, 200]),很明显会发生什么.您将数据集加载到变量中并使其具有标准长度(在这种情况下为200).在这种情况下,输入变量和2类都将转换为一键向量.

I use the tflearn as a wrapper, as it does all the initialization and other higher level stuff automatically. For simplicity, let's consider this example program. Until line 42, net = tflearn.input_data([None, 200]), it's pretty clear what happens. You load a dataset into variables and make it of a standard length (in this case, 200). Both the input variables and also the 2 classes are, in this case, converted to one-hot vectors.

LSTM如何获取输入? 它可以预测多少个样本的输出?

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

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

我的目标是复制 纸张.例如,我想输入4096个向量作为LSTM的输入,其想法是取16个这样的向量,然后产生分类结果.我认为代码看起来像这样,但是我不知道应该如何输入LSTM.

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. I think the code would look like this, but I don't know how the input to the LSTM should be given.

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)

推荐答案

基本上,lstm占用一次单元的向量大小:

Basically, lstm takes the size of your vector for once cell:

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

然后,您要输入多少个时间序列?这取决于您的喂食向量. X_split中的数组数量决定了时间步长:

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 = tf.split(0, time_step_size, X)
outputs, states = rnn.rnn(lstm, X_split, initial_state=init_state)

在您的示例中,我猜lstm_size是256,因为它是一个单词的向量大小. time_step_size是您的训练/测试句子中的最大单词数.

In your example, I guess the lstm_size is 256, since it's the vector size of one word. The time_step_size would be the max word count in your training/test sentences.

请参见以下示例: https://github.com/nlintz/TensorFlow-Tutorials/blob/master/07_lstm.py

这篇关于了解Tensorflow LSTM模型输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 20:21