tf.ones  的一些用法

tf.ones([3, 4], tf.int32)
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]], dtype=int32)>
def make_variables(k, initializer):
  return (tf.Variable(initializer(shape=[k], dtype=tf.float32)),
          tf.Variable(initializer(shape=[k, k], dtype=tf.float32)))
v1, v2 = make_variables(3, tf.ones_initializer())
v1
Out[41]: <tf.Variable 'Variable:0' shape=(3,) dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>

v2
Out[42]: 
<tf.Variable 'Variable:0' shape=(3, 3) dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>


make_variables(4, tf.random_uniform_initializer(minval=-1., maxval=1.))
(<tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([ 0.9270878 , -0.5173075 ,  0.05331755,  0.338547  ], dtype=float32)>,
 <tf.Variable 'Variable:0' shape=(4, 4) dtype=float32, numpy=
 array([[-0.5852165 , -0.09796047, -0.15223312,  0.9891472 ],
        [-0.27840734,  0.2589562 ,  0.45455956, -0.8546467 ],
        [-0.19497204, -0.4024372 ,  0.8427613 ,  0.73146725],
        [-0.77300024,  0.7703638 ,  0.6048634 , -0.15460539]],
       dtype=float32)>)

tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.ones_like(tensor)
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 1, 1],
       [1, 1, 1]], dtype=int32)>

x = [1, 2, 3]
y = [4, 5, 6]
X, Y = tf.meshgrid(x, y)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值