免费域名解析网站建设,网站流量怎么提升,wordpress模板在线编辑,天津网上办事大厅官网增加其他功能
一、增加变量显示
1、目的#xff1a;在TensorBoard当中观察模型的参数、损失值等变量值的变化
2、收集变量 不同的变量要用不同的方式收集
#xff08;1#xff09;tf.summary.scalar(name, tensor) 收集对于损失函数和准确率等单值变量#xff0c;name为…增加其他功能
一、增加变量显示
1、目的在TensorBoard当中观察模型的参数、损失值等变量值的变化
2、收集变量 不同的变量要用不同的方式收集
1tf.summary.scalar(name, tensor) 收集对于损失函数和准确率等单值变量name为变量的名字tensor为值
2tf.summary.histogram(name, tensor) 收集高维度的变量参数
3tf.summary.image(name, tensor) 收集输入的图片张量能显示图片
3、合并变量写入事件文件 1merged tf.summary.merge_all()
4、修改代码
import os
os.environ[TF_CPP_MIN_LOG_LEVEL]2
import tensorflow as tfdef tensorflow_demo():TensorFlow的基本结构# TensorFlow实现加减法运算a_t tf.constant(2)b_t tf.constant(3)c_t a_t b_tprint(TensorFlow加法运算结果\n, c_t)print(c_t.numpy())# 2.0版本不需要开启会话已经没有会话模块了return Nonedef graph_demo():图的演示# TensorFlow实现加减法运算a_t tf.constant(2)b_t tf.constant(3)c_t a_t b_tprint(TensorFlow加法运算结果\n, c_t)print(c_t.numpy())# 查看默认图# 方法1调用方法default_g tf.compat.v1.get_default_graph()print(default_g\n, default_g)# 方法2查看属性# print(a_t的图属性\n, a_t.graph)# print(c_t的图属性\n, c_t.graph)# 自定义图new_g tf.Graph()# 在自己的图中定义数据和操作with new_g.as_default():a_new tf.constant(20)b_new tf.constant(30)c_new a_new b_newprint(c_new\n, c_new)print(a_new的图属性\n, a_new.graph)print(b_new的图属性\n, b_new.graph)# 开启new_g的会话with tf.compat.v1.Session(graphnew_g) as sess:c_new_value sess.run(c_new)print(c_new_value\n, c_new_value)print(我们自己创建的图为\n, sess.graph)# 可视化自定义图# 1创建一个writerwriter tf.summary.create_file_writer(./tmp/summary)# 2将图写入with writer.as_default():tf.summary.graph(new_g)return Nonedef session_run_demo():feed操作tf.compat.v1.disable_eager_execution()# 定义占位符a tf.compat.v1.placeholder(tf.float32)b tf.compat.v1.placeholder(tf.float32)sum_ab tf.add(a, b)print(a\n, a)print(b\n, b)print(sum_ab\n, sum_ab)# 开启会话with tf.compat.v1.Session() as sess:print(占位符的结果\n, sess.run(sum_ab, feed_dict{a: 1.1, b: 2.2}))return Nonedef tensor_demo():张量的演示tensor1 tf.constant(4.0)tensor2 tf.constant([1, 2, 3, 4])linear_squares tf.constant([[4], [9], [16], [25]], dtypetf.int32)print(tensor1\n, tensor1)print(tensor2\n, tensor2)print(linear_squares\n, linear_squares)# 张量类型的修改l_cast tf.cast(linear_squares, dtypetf.float32)print(before\n, linear_squares)print(l_cast\n, l_cast)return Nonedef variable_demo():变量的演示a tf.Variable(initial_value50)b tf.Variable(initial_value40)c tf.add(a, b)print(a\n, a)print(b\n, b)print(c\n, c)with tf.compat.v1.variable_scope(my_scope):d tf.Variable(initial_value30)e tf.Variable(initial_value20)f tf.add(d, e)print(d\n, d)print(e\n, e)print(f\n, f)return Nonedef linear_regression():自实现一个线性回归# 1、准备数据x tf.random.normal(shape[100,1])y_true tf.matmul(x, [[0.8]]) 0.7# 2、构造模型# 定义模型参数用变量weights tf.Variable(initial_valuetf.random.normal(shape[1, 1]))bias tf.Variable(initial_valuetf.random.normal(shape[1, 1]))y_predict tf.matmul(x, weights) bias# 3、构造损失函数error tf.reduce_mean(tf.square(y_predict - y_true))# 4、优化器#optimizer tf.train.GradientDescentOptimizer(learning_rate0.01).minimize(error)optimizer tf.keras.optimizers.SGD(learning_rate0.01)# 创建事件文件file_writer tf.summary.create_file_writer(./tmp/summary)# 收集变量with file_writer.as_default():tf.summary.experimental.set_step(0)# 记录标量变量tf.summary.scalar(error, error)# 记录变量的直方图tf.summary.histogram(weights, weights)tf.summary.histogram(bias, bias)# 5、查看初始化模型参数之后的值print(训练前模型参数为权重%f偏置%f损失%f % (weights, bias, error))# 6、开始训练num_epoch 200 # 定义迭代次数for e in range(num_epoch): # 迭代多次with tf.GradientTape() as tape:y_predict tf.matmul(x, weights) biaserror tf.reduce_mean(tf.square(y_predict - y_true))#error loss_function(y_predict, y_true)grads tape.gradient(error, [weights, bias]) # 求损失关于参数weights、bias的梯度optimizer.apply_gradients(grads_and_varszip(grads, [weights, bias])) # 自动根据梯度更新参数即利用梯度信息修改weights与bias使得损失减小# 每个步骤记录变量with file_writer.as_default():tf.summary.experimental.set_step(e 1)# 记录标量变量tf.summary.scalar(error, error)# 记录变量的直方图tf.summary.histogram(weights, weights)tf.summary.histogram(bias, bias)file_writer.close()print(训练后模型参数为权重%f偏置%f损失%f % (weights, bias, error))return Noneif __name__ __main__:# 代码1TensorFlow的基本结构# tensorflow_demo()# 代码2图的演示#graph_demo()# feed操作#session_run_demo()# 代码4张量的演示#tensor_demo()# 代码5变量的演示#variable_demo()# 代码6自实现一个线性回归linear_regression() 运行结果
训练前模型参数为权重1.398883偏置-0.596879损失1.965775
训练后模型参数为权重0.823115偏置0.676830损失0.001003
5、查看TensorBoard 二、TensorFlow2.0如何显示静态图
1、在TensorFlow1.0时代采用的是静态计算图需要先使用TensorFlow的各种算子创建计算图然后再开启一个会话Session显式执行计算图
2、而在TensorFlow2.0时代采用的是动态计算图即每使用一个算子后该算子会被动态加入到隐含的默认计算图中立即执行得到结果而无需开启Session
3、如果需要在TensorFlow2.0中使用静态图可以使用tf.function装饰器将普通Python函数转换成对应的TensorFlow计算图构建代码。运行该函数就相当于在TensorFlow1.0中用Session执行代码。使用tf.function构建静态图的方式叫做Autograph
4、计算图简介 计算图由节点(nodes)和线(edges)组成 节点表示操作符Operator或者称之为算子线表示计算间的依赖 实线表示有数据传递依赖传递的数据即张量 虚线通常可以表示控制依赖即执行先后顺序
5、因为代码里用到了变量没法用tf.function把静态图弄出来