TensorFlow直接print Tensor拿到的是结构不是真实值。
暂时没有想到好方法,如果读者有更好的方法也请留言给博主,共同学习,谢谢。
对于线性程序:
import tensorflow as tf
import tensorflow.contrib.layers as layers
import numpy as np
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='3'
obs = np.array([-3.45687799e+01, -7.04237729e+02, 5.00000000e+02, -1.22464680e-13, 0.00000000e+00, 0.00000000e+00])
datain = tf.placeholder(tf.float32, shape=(1,6), name="in")
out1 = layers.fully_connected(datain, num_outputs=64, activation_fn=tf.nn.relu)
out2 = layers.fully_connected(out1, num_outputs=64, activation_fn=tf.nn.relu)
out3 = layers.fully_connected(out2, num_outputs=64, activation_fn=tf.nn.relu)
out4 = layers.fully_connected(out3, num_outputs=1, activation_fn=tf.nn.tanh)
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
prt0 = sess.run(datain, feed_dict={datain: [obs]})
print("prt0:",prt0)
prt1 = sess.run(out1, feed_dict={datain: [obs]})
print("prt1:",prt1)
prt2 = sess.run(out2, feed_dict={datain: [obs]})
print("prt2:",prt2)
prt3 = sess.run(out3, feed_dict={datain: [obs]})
print("prt3:",prt3)
prt4 = sess.run(out4, feed_dict={datain: [obs]})
print("prt4:",prt4)
对于复杂程序,比如我们封装了业务代码,并不是像原本这样的线性结构:model作为函数变量又传进了一个函数…对update方法也有相关的封装处理,我们可以用类似上述的方法,新建一个config文件,用于跨文件共享变量
def model(input, num_outputs, scope, reuse=False, num_units=64):
with tf.variable_scope(scope, reuse=reuse):
out = input
print(out)
out = layers.fully_connected(out, num_outputs=num_units, activation_fn=tf.nn.relu)
out = layers.fully_connected(out, num_outputs=num_units, activation_fn=tf.nn.relu)
out = layers.fully_connected(out, num_outputs=num_outputs, activation_fn=tf.nn.relu)
out = tf.nn.softmax(out)
return out
在config.py中:
class Config:
flag = False
p_func = None
p_input_ph = None
flag 标记是否开始训练,后俩为某一tensorflow网络的输入和输出 通过在对应封装代码里:
Config.p_input_ph = obs_ph
Config.p_func = p
然后在主循环中:
if Config.p_func is not None:
sess = U.get_session()
print("[debug]:p:",sess.run(Config.p_func,feed_dict={Config.p_input_ph: [obs]}))
def get_session():
"""Returns recently made Tensorflow session"""
return tf.get_default_session()
|