分享一段有报错的代码,希望万能的博友能帮我找找错误在哪(代码出处源于TF实战谷歌深度学习框架)
import tensorflow as tf
import numpy as np
import reader
DATA_PATH = "/home/cvx/Downloads/PTB/simple-examples/forme"
HIDDEN_SIZE = 200 # ---隐藏层的规模
NUM_LAYERS = 2 # ---LSTM结构的层数
VOCAB_SIZE = 10000 # ---单词标识符总共一万个单词
LEARNING_RATE = 1.0
TRAIN_BATCH_SIZE = 20
TRAIN_NUM_STEP = 35
EVAL_BATCH_SIZE = 1
EVAL_NUM_STEP = 1
NUM_EPOCH = 2 # ---使用训练数据的轮数
KEEP_PROB = 0.5 # ---节点不被dropout的概率
MAX_GRAD_NORM = 5 # ---用于控制梯度膨胀的参数
# 定义一个类来描述模型结构
class PTBModel(object):
def __init__(self, is_training, batch_size, num_step):
self.batch_size = batch_size
self.num_step = num_step
# 定义输入层
self.input_data = tf.placeholder(dtype=tf.int32, shape=[batch_size, num_step], name="input")
# 定义输出层
self.output_data = tf.placeholder(tf.int32, [batch_size, num_step], "output")
# 定义LSTM的结构
lstm = tf.nn.rnn_cell.BasicLSTMCell(HIDDEN_SIZE)
if is_training:
lstm = tf.nn.rnn_cell.DropoutWrapper(lstm, KEEP_PROB)
cell = tf.nn.rnn_cell.MultiRNNCell([lstm]*NUM_LAYERS)
self.initial_state = cell.zero_state(batch_size, tf.float32)
# 将单词ID转换为单词向量。
embedding = tf.get_variable("embedding", [VOCAB_SIZE, HIDDEN_SIZE])
inputs = tf.nn.embedding_lookup(embedding, self.input_data)
# 只在训练的时候使用dropout
if is_training:
inputs = tf.nn.dropout(inputs, keep_prob=KEEP_PROB)
outputs = []
state = self.initial_state
with tf.variable_scope("RNN"):
for time_step in range(num_step):
if time_step > 0:
tf.get_variable_scope().reuse_variables()
cell_output, state = cell(inputs[:, time_step, :], state)
outputs.append(cell_output)
# 将输出队列展开成[batch, hidden_size*num_steps]的形状,然后再reshape成[batch*num_steps, hidden_size]
output = tf.reshape(tf.concat(outputs, 1), [-1, HIDDEN_SIZE])
weights = tf.get_variable("weights", shape=[HIDDEN_SIZE, VOCAB_SIZE])
bias = tf.get_variable("bias", shape=VOCAB_SIZE)
logits = tf.matmul(output, weights) + bias
# 定义计算序列的交叉熵损失函数
loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example([logits],
[tf.reshape(self.output_data, [-1])],
[tf.ones([batch_size*num_step], dtype=tf.float32)])
self.cost = tf.reduce_sum(loss)/batch_size
self.final_state = state
# 只在训练时定义反向操作
if not is_training:
return
# 返回需要训练的变量列表
training_variables = tf.trainable_variables()
# 通过tf.clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None) clip_norm是截取的比率,这个函数返回截取过的梯度张量和一个所有张量的全局范数。
# 函数控制梯度的大小,避免梯度膨胀的问题
grads, _ = tf.clip_by_global_norm(tf.gradients(ys=self.cost, xs=training_variables), MAX_GRAD_NORM)
optimizer = tf.train.GradientDescentOptimizer(LEARNING_RATE)
# zip将梯度和变量结合,利用优化器将梯度应用到可训练的参数
self.train_op = optimizer.apply_gradients(zip(grads, training_variables))
# 使用给定的模型model在数据data上运行train_op并返回在全部数据上的perplexity值。
def run_epoch(session, model, data, train_op, output_log):
total_costs = 0.0
iters = 0
state = session.run(model.initial_state)
for step, (x, y) in enumerate(reader.ptb_producer(data, model.batch_size, model.num_step)):
cost, state, _ = session.run([model.cost, model.final_state, train_op],
{model.input_data: x, model.output_data: y,
model.initial_state: state})
total_costs += cost
iters += model.num_step
if output_log and step % 100 == 0:
print("After %d steps, perplexity is %.3f" % (step, np.exp(total_costs/iters)))
return np.exp(total_costs/iters)
def main(_):
train_data, valid_data, test_data, _ = reader.ptb_raw_data(DATA_PATH)
# 定义初始化函数
initializer = tf.random_uniform_initializer(-0.05, 0.05)
# 定义训练用的循环神经网络模型
with tf.variable_scope("language_model", reuse=None, initializer=initializer):
train_model = PTBModel(True, TRAIN_BATCH_SIZE, TRAIN_NUM_STEP)
# 定义测评用的循环神经网络模型
with tf.variable_scope("language_model", reuse=True, initializer=initializer):
eval_model = PTBModel(False, EVAL_BATCH_SIZE, EVAL_NUM_STEP)
with tf.Session() as session:
tf.global_variables_initializer().run()
# 使用训练数据训练模型
for i in range(NUM_EPOCH):
print("In iteration: %d "% (i+1))
run_epoch(session, train_model, train_data, train_model.train_op, True)
# 使用验证数据评测模型效果
valid_perplexity = run_epoch(session, eval_model, valid_data, tf.no_op(), False)
print("Epoch: %d Validation Perplexity: %.3f" % (i+1, valid_perplexity))
# 最后使用测试数据测试模型效果
test_perplexity = run_epoch(session, eval_model, test_data, tf.no_op(), False)
print("Test Perplexity: %.3f" % test_perplexity)
if __name__ == '__main__':
tf.app.run()
# 报错:TypeError: 'Tensor' object is not iterable.