Tensorflow学习笔记(四)变量、张量

一、创建变量

x = tf.Variable()
tf.Variable(initial_value=None, trainable=True, collections=None, validate_shape=True, 
caching_device=None, name=None, variable_def=None, dtype=None, expected_shape=None, 
import_scope=None)
tf.get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, 
trainable=True, collections=None, caching_device=None, partitioner=None, validate_shape=True, 
custom_getter=None)

使用tf.Variable时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()时,系统不会处理冲突,而会报错.
下面是常用的参数使用:

x = tf.Variable(tf.compat.v1.random_normal(([3,4],stddev = 1))

random_normal指的是正态分布,tensorflow中随机数生成函数主要用于初始化,常用的主要有以下四种:
1.random_normal:正态分布
(shape,mean,stddev,dtype,seed,name)
2.truncated_normal:正态分布,但如果随机出来的值偏离平均值两个标准差,那么这个数会被重新随机
(shape,mean,stddev,dtype,seed,name)
3.random_uniform:平均分布
(shape,minval,maxval,dtype,seed,name)
4.random_gamma:Gamma分布
(shape,alpha,beta,stddev,dtype,seed,name)

其他常用的初始化函数:
zero(shape,dtype,name):产生全0数组
ones(shape,dtype,name):产生全1数组
fill(dims,value,name):产生一个值为给定数字的数组
constant(value,dtype,shape,name,verify_shape):产生一个给定值的常量
例如:

biases = tf.Variable(tf.zeros([2,3],int32))
#Int32  意思是32位整数(32bit integer), 相当于 int      占4个字节   -2147483648 ~ 2147483647
#产生一个全0的2x3数组,结果为[[0,0,0],[0,0,0]]
sess.run(biases.initializer)
#初始化biases

initialize_all_variable()函数(global_variables_initializer())可以初始化所有变量:

init_op = tf.initialize_all_variables()
or
init_op = tf.compat.v1.global_variables_initializer()
sess.run(init_op)

示例程序:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
#动态图机制,disable处于不可使用状态,enable可使用状态
x = tf.constant([[1.0,2.0]])
#定义常量
w1 = tf.Variable(tf.compat.v1.random_normal([2,3],stddev = 1, seed = 1))
w2 = tf.Variable(tf.compat.v1.random_normal([3,1],stddev = 1, seed = 1))
#定义变量,变量为随机正态分布
a = tf.matmul(x,w1)
y = tf.matmul(a,w2)
#tf.matmul()将矩阵a乘以矩阵b,生成a * b。
init_op = tf.compat.v1.global_variables_initializer()
#全部变量初始化函数
with tf.compat.v1.Session() as sess:
    sess.run(init_op)
    print(sess.run(y))
    print(sess.run(y))
    print(y)

运行结果如下:
......
......
......
Skipping registering GPU devices...
2020-06-18 10:48:09.970309: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-06-18 10:48:09.980531: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x19f85a4f8e0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-06-18 10:48:09.980943: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-06-18 10:48:09.981294: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-06-18 10:48:09.981591: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      
[[7.2020965]]
[[7.2020965]]
Tensor("MatMul_1:0", shape=(1, 1), dtype=float32)

二、变量与张量

tensorflow中变量被当作一种运算处理。
得到的结果为具有name、dtype、shape等属性的张量。
上面程序中y就代表一个张量。结果如输出所示。

注意:变量在构建以后,它的类型就无法改变。
更改维度的方法:
validate_shape=False
示例:

tf.assign(w1,w2,validate_shape=Falase)
#assign()函数将w2参数的值赋值给w1
tf.assign(ref,value,validate_shape,use_locking,name)

三、管理变量张量

get_variable()函数用于创建/获取张量

a = tf.compat.v1.get_variable(name,shape,dtype,initializer,regularizer,trainable,conllections,caching_device,partitioner,validate_shape,custon_getter)

常用初始化函数:
constant_initializer(): 将变量初始化为给定常量
random_normal_initializer(): 将变量初始化为满足正态分布的随机数值
truncated_normal_initializer():将变量初始化为满足正态分布的随机数值,但随机值超过两个标准差,这个数会被重新随机
random_uniform_intializer(): 将变量初始化为满足平均分布的随机数
uniform_uint_scaling_initializer(): 将变量初始化为满足平均分布但不影响输出量级的随机值
zeros_intializer():将变量初始化为全0
ones_initializer(): 将变量初始化为全1

variable_scope()结合上下文管理器(with)生成一个变量空间。

variable_scope(reuse)
# reuse=False——get_variable()函数将创建新的变量,此时的name属性不可以与已经存在的变量相同
#      =True——get_variable()函数直接获取name属性相同的已经存在的变量无论get_variable()还是variable()都会被添加变量空间的名称前缀

name_scope()结合上下文管理器(with)生成一个变量空间。多用于可视化计算图的时候
使用get_variable()函数生成的变量名称不会添加变量空间的名称前缀,variable()会被添加变量空间的名称前缀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值