当前位置:网站首页>tensorflow中tf.Variable()函数的用法
tensorflow中tf.Variable()函数的用法
2022-07-26 03:10:00 【phac123】
参考文献
简述
tf.Variable()函数用于创建变量(Variable),变量是一个特殊的张量(),其可以是任意的形状和类型的张量。
语法的格式如下:
tf.Variable.init(initial_value, trainable=True,
collections=None, validate_shape=True, name=None))
注意: 在启动图时(进行操作之前),所有的变量必须被明确定义.变量常常用来存储和更新参数,在计算图过程中其数值会一直保存至程序运行结束,这点正式区别一般的张量。一般的Tensorflow张量在运行过程中仅仅是从计算图中流过,并不会被保存下来;涉及到变量的相关操作必须通过session会话进行控制。
特别注意:在tensorflow中变量的定义和初始化是分开的.
初始化变量:之前是用tf.initialize_all_variables()函数,但是在2017年3月2号之后,该函数不再使用,用 tf.global_variables_initializer() 替代 tf.initialize_all_variables()
tf.global_variables_initializer()用于初始化所有变量;w.initializer用于初始化单个变量。
此外: 也可以使用 tf.get_variable ()函数来定义变量。实质上,tf.Variable()是真正的定义变量,而tf.get_variable()是获取变量,主要设计目的是用于共享变量,但如果获取不到就等同于新定义变量。
此外,使用tf.Variable时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()时,系统不会处理冲突,而会报错
此外的此外,使用tf.Variable()时,如若检测到重名现象,系统会自动处理,处理方式一般是在重名后面加上"_n"。然而,tf.get_variable()发现重名冲突直接报错。
实例
使用tf.Variable()创建变量
import tensorflow._api.v2.compat.v1 as tf
tf.disable_v2_behavior()
w = tf.Variable([6,8,6]) #创建变量w
print(w) #查看变量的shape,而不是值。
with tf.Session() as sess:
sess.run(w.initializer) #初始化变量
print(sess.run(w)) #查看变量的值
输出:
''' <tf.Variable 'Variable:0' shape=(3,) dtype=int32_ref> [6 8 6] '''
tf.get_variable()与tf.Variable()
import tensorflow._api.v2.compat.v1 as tf
tf.disable_v2_behavior()
import tensorflow as tf
V1 = tf.Variable([1,2,3],name="V1")
V2 = tf.Variable([2,3,4],name="V1")
V3 = tf.Variable([3,4,5],name="V1_1")
print("V1:",V1.name)
print("V2:",V2.name)
print("V3:",V3.name)
输出:
''' V1: V1:0 V2: V1_1:0 V3: V1_1_1:0 '''
小结
- 一般情况使用参数initial_value与name就足够了
- tf.get_variable()与tf.Variable()都用来定义变量的
边栏推荐
- Use eventlog analyzer for log forensics analysis
- Win11 method of changing disk drive letter
- Etcdv3 actual combat (III) -prevkv description and related operations
- Pit trodden when copying list: shallow copy and deep copy
- Implement a method to find the sum of the number k and m in the array
- [translation] cloud like internal load balancer for kubernetes?
- [sql] case expression
- Autojs cloud control source code + display
- snownlp库各功能及用法
- Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
猜你喜欢

班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学

VR panoramic shooting and production of business center helps businesses effectively attract people

FPGA_ Initial use process of vivado software_ Ultra detailed

实现一个方法,找出数组中的第k大和第m大的数字相加之和

Arthas view the source code of the loaded class (JAD)

重装Win7系统如何进行?

LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
![[NOIP2001 普及组]装箱问题](/img/b7/1310b3e68d0ee016465fc069315af6.png)
[NOIP2001 普及组]装箱问题

OxyCon 2022 网络抓取前沿大会即将开启!

Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
随机推荐
Teach you to rely on management hand in hand
班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学
Qt 信号在多层次对象间传递 多层嵌套类对象之间信号传递
Win11麦克风权限的开启方法
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
78. 子集
QT signal transmission between multi-level objects signal transmission between multi-level nested class objects
els 窗口设置、WM_CREATE、WM_PAINT
Swin Transformer【Backbone】
万维网、因特网和互联网的区别
C language layered understanding (C language function)
How to install with USB flash disk?
STM32——PWM学习笔记
STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
YOLOv3: An Incremental Improvement
JVM内存模型解析
[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
els 回调函数、退出消息
Implement a method to find the sum of the number k and m in the array
手把手教你依赖管理