tensorflow中的变量和占位符
来源:互联网 发布:淘宝美工可以用素材网 编辑:程序博客网 时间:2024/05/29 05:02
import tensorflow as tfa = tf.constant(2, tf.int16)b = tf.constant(4, tf.float32)graph = tf.Graph()with graph.as_default(): a = tf.Variable(8, tf.float32) b = tf.Variable(tf.zeros([2, 2], tf.float32))with tf.Session(graph=graph) as session: tf.global_variables_initializer().run() print(session.run(a)) print(session.run(b))
实验结果:
8[[ 0. 0.] [ 0. 0.]]
tensorflow在图graph中定义了a,b两个变量,在启动graph时候,必须把变量加载到内存中(通过方法global_variables_initializer())
,这样才能在session中run(a),run(b)
session只能启动graph=graph中的变量,如果变量不在graph中就会报错。
import tensorflow as tfgraph = tf.Graph()with graph.as_default(): a = tf.Variable(8, tf.float32) b = tf.Variable(tf.zeros([2, 2], tf.float32))a = tf.constant(2, tf.int16)b = tf.constant(4, tf.float32)with tf.Session(graph=graph) as session: tf.global_variables_initializer().run() print(session.run(a)) print(session.run(b))
报错
ValueError: Fetch argument <tf.Tensor 'Const:0' shape=() dtype=int16> cannot be interpreted as a Tensor. (Tensor Tensor("Const:0", shape=(), dtype=int16) is not an element of this graph.)
变量必须初始化才会有具体的值global_variables_initializer()进行初始化,而常量就不用初始化。
占位符
占位符是定义一个可变的常量,占位符赋值后不用初始化就可以获取值。
import tensorflow as tfx = tf.placeholder(tf.string)y = tf.placeholder(tf.int32)z = tf.placeholder(tf.float32)with tf.Session() as sess: output = sess.run(x, feed_dict={x: 'Test String', y: 123, z: 45.67})print(output)
实验结果:
Test String
阅读全文
0 0
- tensorflow中的变量和占位符
- tensorflow: 如何使用占位符与变量
- Tensorflow入门----占位符、常量和Session
- Tensorflow入门----占位符、常量和Session
- TensorFlow入门教程(2)占位符、变量、损失函数
- c#中的“+”和占位符
- float、double、long double类型的变量在printf()和scanf()中的占位符使用区别
- sql语句中的#{}占位符和${}占位符
- Tensorflow学习: Placeholder占位符
- 用占位符%p输出变量在内存中的具体地址
- mybatis中的sql语句中的#{}占位符和${}占位符的区别及遇到的问题
- 查看TensorFlow checkpoint文件中的变量名和对应值
- 查看tensorflow ckpt文件中的变量名和对应值
- 查看tensorflow ckpt文件中的变量名和对应值
- tensorflow-placeholder-占位符的使用
- Maven中的占位符
- OC中的占位符
- Android中的占位符
- 怎样解决word项目编号,第10个编号后的缩进量比前9个大的问题?
- Werkzeug 之数据结构解读不可变类
- Java Persistence with MyBatis 3(中文版) 第二章 引导MyBatis
- 重定向和转发的区别
- HashMap原理详解
- tensorflow中的变量和占位符
- B. Train Seats Reservation 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛
- gcd&exgcd
- Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器
- 使用阿里巴巴图标库生成iconfont字体图标
- XILINX K7 DDR3引脚验证总结
- 设备模型
- angular4实战(2) router
- DFS的应用——Binary Tree Paths