我就废话不多说了,直接上代码吧!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#全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 ])
# 和v4_1形状一样的全1或全0矩阵
v5_1 = tf.ones_like(v4_1)
v5_2 = tf.zeros_like(v4_1)
#生成等差数列
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" ))
#正态分布,但是去掉2sigma外的数字
v8_3 = tf.Variable(tf.truncated_normal([ 2 , 3 ], mean = 0.0 , stddev = 1.0 , dtype = tf.float32, seed = 1234 , name = "v8_3" ))
#把这3个行重排列
v8_5 = tf.random_shuffle([[ 1 , 2 , 3 ],[ 4 , 5 , 6 ],[ 6 , 6 , 6 ]], seed = 134 , name = "v8_5" )
|
以上都是计算图中的变量,需要sess.run()以后才能成为真正的数据
存取方式是:
1
2
3
|
np.save( "v1.npy" ,sess.run(v1)) #numpy save v1 as file
test_a = np.load( "v1.npy" )
print test_a[ 1 , 2 ]
|
这篇Tensorflow的常用矩阵生成方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/windows2/article/details/78664779