TensorFlow创建tensor语句

学习课程
1.np.array转tf:

tf.convert_to_tensor(np.ones(2,1))
tf.convert_to_tensor([2,1])
tf.convert_to_tensor([[2],[1]])

2.tf.zeros

tf.zeros([])  #一个数
tf.zeros([2])  #一个向量
tf.zeros([2,2])  #一个矩阵
tf.zeros([2,3,3])

3.tf.zeros_like

a = tf.zeros([2,1])
tf.zeros_like(a)  #和a大小一致的tensor

tf.zeros(a.shape)

4.tf.ones

tf.ones(1)  #一维向量
tf.ones([])  #一个数
tf.ones([2]) #二维向量
tf.ones([2,2])
tf.ones_like(a)

5.Fill

tf.fill([2.3],2)  #2*3矩阵。每个数都是2

6.Normal

tf.random.normal([2,3],mean=1,stddev=1) #2*3矩阵,每个数服从均值为1 方差为1的正态分布
tf.random.trunated_normal([2,3],mean=1,stddev=1)

7.uniform

tf.random.uniform([2,3],minval=0,maxval=100,tf.dtype = tf.int32) #均匀分布

8.Random Permutation

idx = tf.range(10)
idx = tf.random.shuffle(idx)  #随机打散

9.tf.constant

tf.tf.constant(1) #数值为1
tf.tf.constant([1]) #向量为[1]
tf.tf.constant([1,2]) #向量为[1,2]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值