关于ptb\u word函数run\u epoch的几个问题_lm.py公司张量流rnn tuori

2024-04-18 07:57:40 发布

您现在位置:Python中文网/ 问答频道 /正文

tensorflow rnn ptb语言模型tuorial https://github.com/tensorflow/models/tree/master/tutorials/rnn/ptb

我有两个关于ptb\u word中的run\u epoch函数的问题_lm.py公司,仅cpu设备

for step in range(model.input.epoch_size):
feed_dict = {}
for i, (c, h) in enumerate(model.initial_state):
  feed_dict[c] = state[i].c
  feed_dict[h] = state[i].h

vals = session.run(fetches, feed_dict)
cost = vals["cost"]
state = vals["final_state"]

问题1:为什么这里需要为sess创建一个提要,我认为在PTBmodel类中,它已经为lstm网络创建了初始状态

cell = tf.contrib.rnn.MultiRNNCell(
    [cell for _ in range(config.num_layers)], state_is_tuple=True)
self._initial_state = cell.zero_state(config.batch_size, data_type())
state = self._initial_state

outputs = []
with tf.variable_scope("RNN"):
  for time_step in range(self.num_steps):
    if time_step > 0: tf.get_variable_scope().reuse_variables()
    (cell_output, state) = cell(inputs[:, time_step, :], state)
    outputs.append(cell_output)

问题2:为什么可以sessrion.run运行(fetches,feed\u dict)在这里返回值,但是,我在测试代码中尝试了这个方法,它不返回任何值

import tensorflow as tf

# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)

# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
# train = optimizer.minimize(loss)

# training data
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
# training loop
init = tf.global_variables_initializer()

tvars = tf.trainable_variables()
grads = tf.gradients(loss, tvars)
train = optimizer.apply_gradients(
            zip(grads, tvars))
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(100):
  print sess.run(train, {x: x_train, y: y_train})

它只是打印出来的

None
None
None
..
..

谢谢你!你知道吗


Tags: runinformodeltffeedstepcell