标签:div highlight ace path rap int python 结构 const
import tensorflow as tf
# 构造图的结构
# 使用一个线性方程 y = w * x + b
w = tf.Variable(2.0, dtype=tf.float32, name=‘weight‘) # 权重
b = tf.Variable(1.0, dtype=tf.float32, name=‘bias‘) # 偏差
# 占位符,之后添加值
x = tf.placeholder(dtype=tf.float32, name=‘input‘) # 输入
with tf.name_scope(‘output‘):
y = w * x + b # 输出
# 定义保存日志的路径
path = ‘./log‘
# 一旦定义Variable一定要初始化,不同于constant,定义constant自动初始化
# 创建用于初始化所有Variable的操作,而且init也同样需要run一下。
init = tf.global_variables_initializer()
# 创建session
with tf.Session() as sess:
sess.run(init) # 实现初始化变量,必须要run,权重和偏差就真正的被初始化了
# 用于tensorboard展示,传入日志路径和graph
writer = tf.summary.FileWriter(path, sess.graph)
# run,之前的x还没赋值,所以使用feed_dict传值
result = sess.run(y, feed_dict={x: 3.0})
print(f‘y={result}‘) # 打印y = w*x + b的值
‘‘‘
y=7.0
‘‘‘

执行程序之后,会多出一个log文件夹
用tensorboard打开

标签:div highlight ace path rap int python 结构 const
原文地址:https://www.cnblogs.com/traditional/p/9339430.html