【发布时间】:2017-09-08 18:49:20
【问题描述】:
我一直在研究用于语言模型的 RNN,并在一本书中找到了它与 Tensorflow 的代码。
但我真的不明白x[:t:] 在下面的代码中做了什么.....
我是机器学习的初学者,如果有人知道,请给我一个线索。
=======代码========
def inference(x, n_batch, maxlen=None, n_hidden=None, n_out=None):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.zeros(shape, dtype=tf.float32)
return tf.Variable(initial)
cell = tf.contrib.rnn.BasicRNNCell(n_hidden)
initial_state = cell.zero_state(n_batch, tf.float32)
state = initial_state
outputs = []
with tf.variable_scope('RNN'):
for t in range(maxlen):
if t > 0:
tf.get_variable_scope().reuse_variables()
(cell_output, state) = cell(x[:, t, :], state)
outputs.append(cell_output)
output = outputs[-1]
V = weight_variable([n_hidden, n_out])
c = bias_variable([n_out])
y = tf.matmul(output, V) + c
return y
【问题讨论】:
-
x[:t:] 还是 x[:, t, :]?
-
这完全取决于您没有提供的
x的类型。 -
@timgeb -- 基于标签,我敢打赌它几乎肯定是一个张量流张量(它的行为很像一个 numpy 数组)。
标签: python numpy tensorflow