我将Tensorboard添加到现有的小Tensorflow项目中,我知道该项目可以练习使用Tensorboard,但是我得到一个类型错误,global_step必须是字符串或张量,但是我已经将global_step分配给tf.Variable(0, name='global_step', trainable=False)
就像文档和我在网上看到的每个例子一样。对我所缺少的任何想法都会非常感激。
---> 70 [summary_merge,tf.train.global_step(sess,global_step_tensor),update_model,weights],feed_dict)
TypeError:Fetch参数0具有无效类型,必须是字符串或Tensor。 (无法将int转换为Tensor或Operation。)
import gym
import numpy as np
import random
import ipdb
import matplotlib.pyplot as plt
import tensorflow as tf
%matplotlib inline
tf.reset_default_graph()
env = gym.make('FrozenLake-v0')
global_step_tensor = tf.Variable(0, name='global_step', trainable=False)
saver = tf.train.Saver()
with tf.name_scope('policy-network'):
observations = tf.placeholder(shape=[1, env.observation_space.n], dtype=tf.float32, name='input')
weights = tf.Variable(tf.random_uniform([16,4], 0, 0.01))
Qout = tf.matmul(observations, weights)
predict = tf.argmax(Qout, 1)
nextQ = tf.placeholder(shape=[1, 4], dtype=tf.float32)
loss = tf.reduce_sum(tf.square(nextQ - Qout))
trainer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
update_model = trainer.minimize(loss, global_step= global_step_tensor)
with tf.name_scope('summaries'):
summary_merge = tf.summary.merge([
tf.summary.histogram('loss', loss),
tf.summary.histogram('weights', weights)
])
init = tf.global_variables_initializer()
gamma = .99
epsilon = 0.1
num_episodes = 2000
j_list = []
r_list = []
with tf.Session() as sess:
# Create a writer for summary data
writer = tf.summary.FileWriter('/tmp/display', sess.graph)
sess.run(init)
for i in range(num_episodes):
s = env.reset()
reward_all = 0
done = False
j = 0
while j < 99:
j+= 1
a, allQ = sess.run([predict, Qout], feed_dict={observations:np.identity(16)[s:s+1]})
if np.random.rand(1) < epsilon:
a[0] = env.action_space.sample()
s1, reward, done, _ = env.step(a[0])
Q1 = sess.run(Qout, feed_dict={observations: np.identity(16)[s1:s1+1]})
maxQ1 = np.max(Q1)
targetQ = allQ
targetQ[0, a[0]] = reward + gamma*maxQ1
feed_dict = {observations: np.identity(16)[s:s+1], nextQ:targetQ}
# Run prediction operation, evaluate summary_merge and assign to summaries
summaries, global_step_tensor, _, W1 = sess.run(
[summary_merge, tf.train.global_step(sess, global_step_tensor) ,update_model, weights], feed_dict)
# Write summaries to writer
writer.add_summary(summaries, global_step)
reward_all += reward
s = s1
if done == True:
epsilon = 1./((i/50) + 10)
break
if (i+1) % 1000 == 0:
# Save the graph every 1000 episodes
saver.save(sess, '/tmp/checkpoint', global_step=global_step)
j_list.append(j)
r_list.append(reward_all)
print("Percent of succesful episodes: {} %".format((sum(r_list)/num_episodes)))