tf.Variable()函数
tf.Variable(initializer,name),参数initializer是初始化参数,name是可自定义的变量名称,用法如下:
import tensorflow as tfv1=tf.Variable(tf.random_normal(shape=[4,3],mean=0,stddev=1),name='v1')
v2=tf.Variable(tf.constant(2),name='v2')
v3=tf.Variable(tf.ones([4,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))
结果如下:
[[-1.2115501 1.0484737 0.55210656][-1.5301195 0.9060654 -2.6766613 ][ 0.27101386 -0.32336152 0.44544214][-0.0120788 -0.3409422 -0.48505628]]
2
[[1. 1. 1.][1. 1. 1.][1. 1. 1.][1. 1. 1.]]
在TensorFlow的世界里,变量的定义和初始化是分开的,所有关于图变量的赋值和计算都要通过tf.Session的run来进行。想要将所有图变量进行集体初始化时应该使用tf.global_variables_initializer。
生成tensor:
tf.zeros(shape, dtype=tf.float32, name=None)
tf.zeros_like(tensor, dtype=None, name=None)
tf.constant(value, dtype=None, shape=None, name='Const')
tf.fill(dims, value, name=None)
tf.ones_like(tensor, dtype=None, name=None)
tf.ones(shape, dtype=tf.float32, name=None)
生成序列
tf.range(start, limit, delta=1, name='range')
tf.linspace(start, stop, num, name=None)
生成随机数
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_shuffle(value, seed=None, name=None)
例子
import tensorflow as tf
import numpy as np# 生成0和1矩阵
v1 = tf.Variable(tf.zeros([3, 3, 3]), name="v1")
v2 = tf.Variable(tf.ones([10, 5]), name="v2")
# 填充单值矩阵
v3 = tf.Variable(tf.fill([2, 3], 9))
# 常量矩阵
v4_1 = tf.constant([1, 2, 3, 4, 5, 6, 7])
v4_2 = tf.constant(-1.0, shape=[2, 3])
# 生成等差数列
v6_1 = tf.linspace(10.0, 12.0, 30, name="linspace") # float32 or float64
v7_1 = tf.range(10, 20, 3) # just int32
# 生成各种随机数据矩阵
v8_1 = tf.Variable(tf.random_uniform([2, 4], minval=0.0, maxval=2.0, dtype=tf.float32, seed=1234, name="v8_1"))
v8_2 = tf.Variable(tf.random_normal([2, 3], mean=0.0, stddev=1.0, dtype=tf.float32, seed=1234, name="v8_2"))
v8_3 = tf.Variable(tf.truncated_normal([2, 3], mean=0.0, stddev=1.0, dtype=tf.float32, seed=1234, name="v8_3"))
v8_4 = tf.Variable(tf.random_uniform([2, 3], minval=0.0, maxval=1.0, dtype=tf.float32, seed=1234, name="v8_4"))
v8_5 = tf.random_shuffle([[1, 2, 3], [4, 5, 6], [6, 6, 6]], seed=134, name="v8_5")# 初始化
init_op = tf.initialize_all_variables()
# 保存变量,也可以指定保存的内容
saver = tf.train.Saver()
# saver = tf.train.Saver({"my_v2": v2})# 运行
with tf.Session() as sess:sess.run(init_op)# 输出形状和值print(tf.Variable.get_shape(v1))# shapeprint(sess.run(v1))# vaule# numpy保存文件np.save("v1.npy", sess.run(v1)) # numpy save v1 as filetest_a = np.load("v1.npy")print(test_a[1, 2])# 一些输出print(sess.run(v3))v5 = tf.zeros_like(sess.run(v1))print(sess.run(v6_1))print(sess.run(v7_1))print(sess.run(v8_5))# 保存图的变量save_path = saver.save(sess, "/tmp/model.ckpt")# 加载图的变量# saver.restore(sess, "/tmp/model.ckpt")print("Model saved in file: ", save_path)
初始化
此处使用tf.initialize_all_variables() 用于初始化变量。但是这句话仍然不会立即执行。需要通过sess来将数据流动起来 。
例子
# Create two variables.
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),name="weights")
biases = tf.Variable(tf.zeros([200]), name="biases")
...
# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()# Later, when launching the model
with tf.Session() as sess:# Run the init operation.sess.run(init_op)...# Use the model...
实例讲解
例子
#-*-coding:UTF-8-*-
import tensorflow as tfstate = tf.Variable(0 , name='counter')
one = tf.constant(1)new_value = tf.add(state,one)
update = tf.assign(state,new_value)init = tf.initialize_all_variables()with tf.Session() as sess:sess.run(init)for _ in range(3): sess.run(update) print(sess.run(state))
#-*-coding:UTF-8-*-
这句话是指定*.py的编码方式,如果文件中涉及到中文汉字的话,有必要写一下这句话。当然也可以这样写:encoding:UTF-8
import tensorflow as tf
这句话是导入tensorflow 模块
state = tf.Variable(0 , name='counter')
使用tensorflow在默认的图中创建节点,这个节点是一个变量。
one = tf.constant(1)
此处调用了td的一个函数,用于创建常量。
update = tf.assign(state,new_value)
这个操作是:赋值操作。将new_value的值赋值给update变量。
在这里,需要再次说明:我们此时只是定义好了图,并没有变量并没有初始化。目前只有state的值是1。
with tf.Session() as sess:
此处自动开启一个session
sess.run(init)
对变量进行初始化,执行(run)init语句
for _ in range(3): sess.run(update) print(sess.run(state))
循环3次,并且打印输出。
tf.Variable()函数相关推荐
- tensorflow中tf.get_variable()函数详解
如果变量存在,函数tf.get_variable()会返回现有的变量:如果变量不存在,会根据给定形状和初始值创建一个新的变量. def get_variable(name, shape=None, d ...
- TF:tensorflow框架中常用函数介绍—tf.Variable()和tf.get_variable()用法及其区别
TF:tensorflow框架中常用函数介绍-tf.Variable()和tf.get_variable()用法及其区别 目录 tensorflow框架 tensorflow.Variable()函数 ...
- TF.VARIABLE和TENSOR的区别(转)
刷课过程中思考到Variable和Tensor之间的区别,尝试发现在如下代码中: a = tf.Variable(tf.ones(1)) b = tf.add(a,tf.ones(1)) 1 2 a是 ...
- tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...
- tf.pad函数功能介绍
tf.pad()函数主要是对张量在各个维度上进行填充,该函数的参数如下所示: pad(tensor,paddings,mode='CONSTANT',name=None ) 其中: tensor是待填 ...
- tf.reduce_max()函数的用法详解
tf.reduce_max()函数 tf.reduce_max(input_tensor,axis=None,name=None,keepdims=False #是否保持矩形原狀 ) 参数解释: in ...
- 记录 之 tf.placeholder() 函数的意义及用法
函数原型: tf.placeholder(dtype, shape=None, name=None) 参数释义:1.dtype:数据类型.常用的是tf.float32,tf.float64等数值类型 ...
- facenet 中心损失函数(center loss)详解(代码分析)含tf.gather() 和 tf.scatter_sub()函数
我们来解读一下,中心损失,再来看代码. 链接:https://www.cnblogs.com/carlber/p/10811396.html 我们的重点是分析代码,所以定义部分,大家详情参见上面的博客 ...
- tf.Variable和 tf.get_variable区别(1)
tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个创建变量函数的区别 先来看看这两个函数的参数列表,就不打了,直接截图 ...
最新文章
- asp.net 在使用Response.Redirect try{}catch{}块失效
- python【Matlibplot绘图库】多图合并显示(真の能看懂~!)
- Codeforces Gym 100676G Training Camp 状压dp
- 在微型计算机中pci指的是一种,2010新疆维吾尔自治区计算机等级考试二级理论考试试题及答案...
- 做乘法c语言,【code】C语言_实现输入的矩阵的乘法运算
- c语言的运算答案,C语言运算符与表达式的练习题答案.doc
- 2021计算机技术调剂,2021年华南师范大学计算机技术考研调剂信息
- RecyclerView 小记
- 【zookeeper】zookeeper 的监听机制
- pbs分解_产品分解结构
- 和电商有关的词语_关于电商的一些词
- 机器学习(八)——SVD推荐系统
- java webview事件_捕获“页面已完成加载”事件,并使用xwalk Webview在ionic / cordova MainActivity.java中进行操作...
- 利用Python生成钢琴音色
- Northwind数据库下载地址
- 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链...
- 计算机电源寿命,影响电脑寿命的几个重要方面
- miscellaneous_7
- nginx配置静态资源为https
- Caffe for Python 官方教程