特殊矩阵初始化
import tensorflow as tf
mat0 = tf.constant([[0,0,0],[0,0,0]])
mat1 = tf.zeros([2,3]) #2行3列空矩阵
mat2 = tf.ones([3,2]) #3行2列单位矩阵
mat3 = tf.fill([2,3],15) #矩阵填充,全填为15
with tf.Session() as sess:
print(sess.run(mat0))
print(sess.run(mat1))
print(sess.run(mat2))
print(sess.run(mat3))
[[0 0 0]
[0 0 0]][[0. 0. 0.]
[0. 0. 0.]][[1. 1.]
[1. 1.]
[1. 1.]][[15 15 15]
[15 15 15]]
import tensorflow as tf
mat1 = tf.constant([[2],[3],[4]])
mat2 = tf.zeros_like(mat1) #与mat1有相同维度,并全为0
mat3 = tf.linspace(0.0,2.0,11) #1-2之间10等分,有11个数据
mat4 = tf.random_uniform([2,3],-1,2) #随机矩阵,2行3列,在-1到2之间产生随机数
with tf.Session() as sess:
print(sess.run(mat2))
print(sess.run(mat3))
print(sess.run(mat4))
[[0]
[0]
[0]]
[ 0. 0.2 0.40000001 0.60000002 0.80000001 1.
1.20000005 1.39999998 1.60000002 1.80000007 2. ]
[[ -1.66177750e-04 7.87446260e-01 -4.10951853e-01]
[ -9.58950400e-01 1.97116375e-01 -2.69421697e-01]]
网友评论