简体   繁体   English

如何在Tensorflow中获得LSTM的密集层输出?

[英]How to get dense layer output of LSTM in Tensorflow?

I am using Tensorflow for modelling an LSTM with a single dense layer. 我正在使用Tensorflow对具有单个密集层的LSTM进行建模。 What I would like to accomplish is to obtain the dense layer output/hidden representations from the LSTM. 我想完成的是从LSTM获得密集层输出/隐藏表示。 I have checked that similar methodology is available in Keras, but how about doing it in Tensorflow? 我已经检查过Keras中可用类似的方法,但是在Tensorflow中如何使用呢? I append my code below which is specific to the problem (referring to LSTM on sequential data, predicting a discrete column ) :- 我在下面附加我的代码,该代码专用于该问题( 在顺序数据上参考LSTM,预测离散列 ):-

# clear graph (if any) before running
tf.reset_default_graph()

X = tf.placeholder(tf.float32, [None, time_steps, inputs], name = "Inputs")
y = tf.placeholder(tf.float32, [None, outputs], name = "Outputs")

# LSTM Cell
cell = tf.contrib.rnn.BasicLSTMCell(num_units=neurons, activation=tf.nn.relu)
cell_outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)

# pass into Dense layer
stacked_outputs = tf.reshape(cell_outputs, [-1, neurons])
out = tf.layers.dense(inputs=stacked_outputs, units=outputs)

# squared error loss or cost function for linear regression
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    labels=y, logits=out))

# optimizer to minimize cost
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)


with tf.Session() as sess:
    # initialize all variables
    tf.global_variables_initializer().run()
    tf.local_variables_initializer().run()

    # Train the model
    for steps in range(epochs):
        mini_batch = zip(range(0, length, batch_size),
                         range(batch_size, length + 1, batch_size))

        # train data in mini-batches
        for (start, end) in mini_batch:
            sess.run(training_op, feed_dict={X: X_train[start:end, :, :],
                                             y: y_train[start:end, :]})

        # print training performance
        if (steps + 1) % display == 0:
            # evaluate loss function on training set
            loss_fn = loss.eval(feed_dict={X: X_train, y: y_train})
            print('Step: {}  \tTraining loss: {}'.format((steps + 1), loss_fn))

The code I have appended is specific to the training set, but I believe the process should be quite similar to feed in a dictionary for the test set. 我所附的代码是特定于训练集的,但是我认为该过程应该与为测试集提供字典非常相似。 Is there any one liner/short code segment which can return the dense layer output (hidden representations of input data). 是否有任何一个衬里/短代码段可以返回密集层输出(输入数据的隐藏表示)。 Any help in this regard is highly appreciated. 在这方面的任何帮助都将受到高度赞赏。

Yes, like this: 是的,像这样:

_, d_out = sess.run([training_op, out], feed_dict={X: X_train[start:end, :, :],
                                         y: y_train[start:end, :]})
print(d_out)

When you are inside a Session context manager, this is the shortest way: out_vals = out.eval({X: X_train}) 当您处于Session上下文管理器中时,这是最短的方法: out_vals = out.eval({X: X_train})

It is equivalent to this: out_vals = sess.run(out, feed_dict={X: X_train}) 等效于此: out_vals = sess.run(out, feed_dict={X: X_train})

You don't need to feed labels for forward propagation (if you just evaluating the dense layer). 您不需要馈送标签进行正向传播(如果您只是评估密集层)。

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

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