首页 文章

了解Tensorflow LSTM模型输入?

提问于
浏览
3

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

我使用tflearn作为包装器,因为它会自动执行所有初始化和其他更高级别的操作 . 为简单起见,我们考虑this example program . 直到line 42net = tflearn.input_data([None, 200]) ,很清楚会发生什么 . 您将数据集加载到变量中并使其成为标准长度(在本例中为200) . 在这种情况下,输入变量和2个类都转换为单热矢量 .

LSTM如何接受输入?它预测输出的样本数量是多少?

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

我的目标是paper中的活动识别数据集 . 例如,我想输入一个4096向量作为LSTM的输入,其思路是采用16个这样的向量,然后产生分类结果 . 我认为代码看起来像这样,但我不知道应该如何给出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)

1 回答

  • 0

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

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

    那么,您想要提供多少个时间序列?这取决于你的喂食矢量 . X_split 中的数组数决定了时间步数:

    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 将是训练/测试句中的最大字数 .

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

相关问题