先贴一段简单的代码,实现的是y = wx + b

# -*- coding: UTF-8 -*-

# 引入tensorflow
import tensorflow as tf

# 构造图(Graph)的结构
# 用一个线性方程的例子 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    # 输出

# const = tf.constant(2.0)  # 不需要初始化

# 定义保存日志的路径
path = "./log"

# 创建用于初始化所有变量(Variable)的操作
# 如果定义了变量,但没有初始化的操作,会报错
init = tf.global_variables_initializer()

# 创建 Session(会话)
with tf.Session() as sess:
    sess.run(init)  # 初始化变量
    writer = tf.summary.FileWriter(path, sess.graph)
    result = sess.run(y, {x: 3.0})  # 为 x 赋值 3
    print("y = W * x + b,值为 {}".format(result))  # 打印 y = W * x + b 的值,就是 7

这段代码在pycharm中运行出来的结果是:

Window7下tensorboard简单使用

同时在当前目录下生成了一个log文件:

                 Window7下tensorboard简单使用

然后打开终端,打开桌面上的tf_test目录,输入命令:tensor --logdir = log

会得到一个网址:

Window7下tensorboard简单使用

将这个网址复制到浏览器打开(我用的Chrome):就会得到可视化图:

Window7下tensorboard简单使用

相关文章:

  • 2022-01-04
  • 2021-09-01
  • 2021-04-26
  • 2021-06-15
  • 2021-12-15
  • 2021-12-19
  • 2022-01-03
猜你喜欢
  • 2018-12-16
  • 2021-06-01
  • 2021-05-27
  • 2021-08-30
  • 2022-02-21
  • 2021-06-16
  • 2021-06-07
相关资源
相似解决方案