@
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/dcrmg/article/details/79028043 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<div class="htmledit_views" id="content_views">
tf.random_normal()函数用于从服从指定正太分布的数值中取出指定个数的值。
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
- shape: 输出张量的形状,必选
- mean: 正态分布的均值,默认为0
- stddev: 正态分布的标准差,默认为1.0
- dtype: 输出的类型,默认为tf.float32
- seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
- name: 操作的名称
以下程序定义一个w1变量:
-
# -*- coding: utf-8 -*-)
-
import tensorflow
as tf
-
-
w1 = tf.Variable(tf.random_normal([
2,
3], stddev=
1, seed=
1))
-
-
with tf.Session()
as sess:
-
sess.run(tf.global_variables_initializer())
-
# sess.run(tf.initialize_all_variables()) #比较旧一点的初始化变量方法
-
print w1
-
print sess.run(w1)
输出:
-
<tf.Variable
'Variable:0' shape=(
2,
3) dtype=float32_ref>
-
[[
-0.81131822
1.48459876
0.06532937]
-
[
-2.4427042
0.0992484
0.59122431]]
变量w1声明之后并没有被赋值,需要在Session中调用run(tf.global_variables_initializer())方法初始化之后才会被具体赋值。
tf中张量与常规向量不同的是执行"print w1"输出的是w1的形状和数据类型等属性信息,获取w1的值需要调用sess.run(w1)方法。