Tensorflow: Math变量初始化

# Tensorflow: Math变量初始化
import tensorflow as tf

# Method 1:
# 1. 使用xxxx()方法生成一个Tensor
# 2. 将这个Tensor作为tf.Variable()中的initial_value参数的入参, 来初始化这个变量
A = tf.Variable(tf.truncated_normal([2, 2], mean=0.0, stddev=1.0, dtype=tf.float32, seed=10))

# Method 2:
# 1. 使用xxxx_initializer()创建一个初始化方法,但并不生成具体的Tensor
# 2. 使用在tf.get_variable()和tf.variable_scope()等方法中, 其中的initializer参数指定为初始化方法
#    则在环境中创建或获取的变量都将使用这个方法进行初始化

# Method 2-tf.get_variable():
initializer_op = tf.truncated_normal_initializer(mean=0.0, stddev=1.0, seed=10, dtype=tf.float32)
B = tf.get_variable(name='B', shape=[2, 2], initializer=initializer_op)

# Method 2-with tf.variable_scope()-tf.get_variable():
with tf.variable_scope("test", initializer=initializer_op):
    C = tf.get_variable("C", shape=[2, 2])

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
print(A.eval())
print(B.eval())
print(C.eval())
sess.close()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值