TensorFlow中的Variable 变量
2024-10-21 05:59:15
简单运用
这节课我们学习如何在 Tensorflow 中使用 Variable
.
在 Tensorflow 中,定义了某字符串是变量,它才是变量,这一点是与 Python 所不同的。
定义语法: state = tf.Variable()
import tensorflow as tf state = tf.Variable(0, name='counter') # 定义常量 one
one = tf.constant(1) # 定义加法步骤 (注: 此步并没有直接计算)
new_value = tf.add(state, one) # 将 State 更新成 new_value
update = tf.assign(state, new_value)
如果你在 Tensorflow 中设定了变量,那么初始化变量是最重要的!!所以定义了变量以后, 一定要定义 init = tf.initialize_all_variables()
.
到这里变量还是没有被激活,需要再在 sess
里, sess.run(init)
, 激活 init
这一步.
# 如果定义 Variable, 就一定要 initialize
# init = tf.initialize_all_variables() # tf 马上就要废弃这种写法
init = tf.global_variables_initializer() # 替换成这样就好 # 使用 Session
with tf.Session() as sess:
sess.run(init)
for _ in range(3):
sess.run(update)
print(sess.run(state))
注意:直接 print(state)
不起作用!!
一定要把 sess
的指针指向 state
再进行 print
才能得到想要的结果!
以上就是我们今天所学的 Variable
打开模式。
最新文章
- [LeetCode] Nth Digit 第N位
- Brew Command Not Found
- Codeforces Round #324 (Div. 2) C (二分)
- Ionic 2.0.0-rc.1 发布,HTML5 移动应用框架
- jquery 按钮效果 正常、移上、按下
- Struts2+Spring3+Mybatis3开发环境搭建
- 【转】SVN服务器搭建--Subversio与TortoiseSVN的配置安装
- bzoj 3207 花神的嘲讽计划Ⅰ(哈希法+主席树)
- tomcat 禁止某些文件(夹)的访问
- 知方可补不足~SQL数据库用户的克隆,SQL集群的用户同步问题
- centos7支持ntfs
- ImageView显示不出来图片
- JavaScript 数组中查找符合条件的值
- Redis字符串类型相关操作命令
- Resources$NotFoundException资源文件没有找到
- EasyARM-iMX283A的make menuconfig出现错误:Install ncurses(ncurses-devel) and try again。
- FZU2150 :Fire Game (双起点BFS)
- 在xampp与phpstorm环境下安装xdebug[转]
- java 线程池线程忙碌且阻塞队列也满了时给一个拒接的详细报告
- gl attribute和uniform的用法
热门文章
- [图像处理] YUV图像处理入门2
- 主线程-创建Thread类的子类
- DNS 是如何影响你冲浪速度的?
- python生成自动化测试报告并发送到指定邮箱
- KingbaseES在线wal日志
- wsl ubuntu vscode 安装 Fira Code
- mysql18-Show Profile和全局日志
- Python3中的“加和”函数
- 2023 年 dotnet 仓库社区年度调查已经开始
- quasar打包时:Module not found: Can't resolve imported dependency ";dayjs/plugin/customParseFormat";