常量
tf.constant()函数
tf.constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
用于定义常量,可以直接传入一个list来初始化 vaule
,也可以指定 value
和 shape
来进行填充。
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) # [1 2 3 4 5 6 7]
tensor = tf.constant(-1.0, shape=[2, 3]) # [[-1. -1. -1.]
# [-1. -1. -1.]]
tf.zeros() 和 tf.ones() 函数
生成全零或全一向量,参数有 shape
、 dtype
和 name
v = tf.ones([2, 3], tf.int32) # [[1, 1, 1], [1, 1, 1]]