1.tf.Variable()

tf.Variable(initializer,name)

功能:tf.Variable()创建变量时,name属性值允许重复,检查到相同名字的变量时,由自动别名机制创建不同的变量。

参数:

  • initializer:初始化参数;
  • name:可自定义的变量名称

举例:

import tensorflow as tf
v1=tf.Variable(tf.random_normal(shape=[2,3],mean=0,stddev=1),name='v1')
v2=tf.Variable(tf.constant(2),name='v2')
v3=tf.Variable(tf.ones([2,3]),name='v3')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(v1))
print(sess.run(v2))
print(sess.run(v3))

结果如下:

2.tf.get_variable()

tf.get_variable(
name,
shape=None,
dtype=None,
initializer=None,
regularizer=None,
trainable=None,
collections=None,
caching_device=None,
partitioner=None,
validate_shape=True,
use_resource=None,
custom_getter=None,
constraint=None,
synchronization=tf.VariableSynchronization.AUTO,
aggregation=tf.VariableAggregation.NONE
)

功能:tf.get_variable创建变量时,会进行变量检查,当设置为共享变量时(通过scope.reuse_variables()或tf.get_variable_scope().reuse_variables()),检查到第二个拥有相同名字的变量,就返回已创建的相同的变量;如果没有设置共享变量,则会报[ValueError: Variable varx alreadly exists, disallowed.]的错误。

参数:

  • name:新变量或现有变量的名称
  • shape:新变量或现有变量的形状
  • dtype:新变量或现有变量的类型(默认为DT_FLOAT)。
  • initializer:变量初始化的方式

初始化方式:

  • tf.constant_initializer:常量初始化函数
  • tf.random_normal_initializer:正态分布
  • tf.truncated_normal_initializer:截取的正态分布
  • tf.random_uniform_initializer:均匀分布
  • tf.zeros_initializer:全部是0
  • tf.ones_initializer:全是1
  • tf.uniform_unit_scaling_initializer:满足均匀分布,但不影响输出数量级的随机值

举例:

v1=tf.Variable(tf.random_normal(shape=[2,3],mean=0,stddev=1),name='v1')
v2=tf.Variable(tf.random_normal(shape=[2,3],mean=0,stddev=1),name='v1')
v3=tf.Variable(tf.ones([2,3]),name='v3') a1 = tf.get_variable(name='a1', shape=[2, 3], initializer=tf.random_normal_initializer(mean=0, stddev=1))
a2 = tf.get_variable(name='a2', shape=[2, 3], initializer=tf.random_normal_initializer(mean=0, stddev=1))
a3 = tf.get_variable(name='a3', shape=[2, 3], initializer=tf.ones_initializer()) with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(sess.run(v1))
print(sess.run(v2))
print(sess.run(v3))
print(sess.run(a1))
print(sess.run(a2))
print(sess.run(a3))

v1和v2的参数完全相同,创建时候不会报错;a1和a2的参数完全相同,创建时候会报错  

3.tf.placeholder()

tf.placeholder(
dtype,
shape=None,
name=None
)

功能:在tensorflow中类似于函数参数,运行时必须传入值。

TensorFlow链接:https://tensorflow.google.cn/api_docs/python/tf/placeholder?hl=en

参数:

  • dtype:要进给的张量中的元素类型。常用的是tf.float32,tf.float64等数值类型。
  • shape:要进给的张量的形状(可选)。如果未指定形状,则可以提供任何形状的张量。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定。
  • name:操作的名称(可选)。

举例:

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32) output = tf.multiply(input1, input2) with tf.Session() as sess:
print(sess.run(output, feed_dict={input1: [23.], input2: [4.]})) # [92.]

  

参考文献:

【1】Tensorflow——tf.Variable()、tf.get_variable()和tf.placeholder()

tf.Variable()、tf.get_variable()和tf.placeholder()的更多相关文章

  1. tensorflow共享变量 the difference between tf.Variable() and get_variable()

    一般这样用tf.get_variable(): v = tf.get_variable(name, shape, dtype, initializer) 下面内容来源于 http://blog.csd ...

  2. tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别

    在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...

  3. TensorFlow 辨异 —— tf.placeholder 与 tf.Variable

    https://blog.csdn.net/lanchunhui/article/details/61712830 https://www.cnblogs.com/silence-tommy/p/70 ...

  4. TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系

    1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要 ...

  5. tf.Variable() 与tf.get_variable()的区别

    每次调用 tf.Variable() 都会产生一个新的变量,变量名称是一个可选参数,运行命名相同,如果命名冲突会根据命名先后对名字进行处理, tf.get_variable()的变量名称是必填参数,t ...

  6. 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope

    tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...

  7. 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同

    https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提 ...

  8. TensorFlow函数(二)tf.get_variable() 和 tf.Variable()

    tf.Variable(<initial - value>,name=<optional - name>) 此函数用于定义图变量.生成一个初始值为initial - value ...

  9. 深度学习原理与框架-Tensorflow基本操作-变量常用操作 1.tf.random_normal(生成正态分布随机数) 2.tf.random_shuffle(进行洗牌操作) 3. tf.assign(赋值操作) 4.tf.convert_to_tensor(转换为tensor类型) 5.tf.add(相加操作) tf.divide(相乘操作) 6.tf.placeholder(输入数据占位

    1. 使用tf.random_normal([2, 3], mean=-1, stddev=4) 创建一个正态分布的随机数 参数说明:[2, 3]表示随机数的维度,mean表示平均值,stddev表示 ...

随机推荐

  1. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  2. linux中sleep函数的使用和总结

    在linux编程中,有时候会用到定时功能,常见的是用sleep(time)函数来睡眠time秒:但是这个函数是可以被中断的,也就是说当进程在睡眠的过程中,如果被中断,那么当中断结束回来再执行该进程的时 ...

  3. 【LGR-060】洛谷10月月赛 I

    A - 打字练习 出题:memset0 送分模拟题,按题意模拟即可. 需要注意的是对退格键的判断,如果光标已经在行首,则直接忽略被读入的退格键. B - 小猪佩奇爬树 出题:_QAQ 维护所有相同节点 ...

  4. [LeetCode] 253. Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. WPF DataGrid 鼠标对表格双击导致客户端崩溃

    该问题是由于在创建DataGrid时没有设置为只读属性 解决:             <DataGrid Name="switchInfoList" MouseLeftBu ...

  6. 搭建Jena Fuseki并执行SPARQL查询

    1. 下载Jena Fuseki:http://jena.apache.org/download/index.cgi 2. 运行服务 windows解压后双击fuseki-server.bat lin ...

  7. Qt Quick 多媒体 - 播放音乐和视频

    MediaPlayer 是 QML 提供的核心多媒体类,可以播放音频.视频.要使用 MediaPlayer,需要引入 QtMultimedia 模块,在 QML 文档的开始加入 "impor ...

  8. jq数字翻页效果,随机数字显示,实现上下翻动效果

    最近在做一个项目,需要实时展示一串数字,要有类似于日历翻页的效果,在网上找寻了一番,发现dataStatistics这个插件http://www.jq22.com/jquery-info8141能实现 ...

  9. Arcpy中Geometry类与Array类转换的陷阱

    1.现象说明 使用Arcpy.da.searchcursor得到Geometry,将Geometry转换成Array,再从Array转换回Geometry.若Geometry包含内环,这个过程可能导致 ...

  10. HandlerInterceptorAdapter

    handler,是指controller的@Controller注解下的整个方法名