TensorFlow2.1创建Tensor

通过Numpy和list来创建Tensor

首先导入库:

import tensorflow as tf
import numpy as np
直接通过convert_to_tensor来创建
  • 通过numpy来传入参数
tf.convert_to_tensor(np.ones([2, 3]))
Out[85]:
<tf.Tensor: shape=(2, 3), dtype=float64, numpy=
array([[1., 1., 1.],
       [1., 1., 1.]])>

tf.convert_to_tensor(np.zeros([2, 3]))
Out[86]:
<tf.Tensor: shape=(2, 3), dtype=float64, numpy=
array([[0., 0., 0.],
       [0., 0., 0.]])>
  • 通过list来传入参数

如果列表中有整型和浮点型的,将所有参数自动升级为浮点型

tf.convert_to_tensor([1, 2])
Out[87]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 2])>

tf.convert_to_tensor(np.zeros([2, 3]))
Out[86]:
<tf.Tensor: shape=(2, 3), dtype=float64, numpy=
array([[0., 0., 0.],
       [0., 0., 0.]])>

tf.convert_to_tensor([1, 2.])
Out[88]: <tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>

tf.convert_to_tensor([[1], [2.]])
Out[89]:
<tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[1.],
       [2.]], dtype=float32)>
直接通过tf.zeros、tf.ones、tf.fill
  • tf.zeros
tf.zeros([])
Out[90]: <tf.Tensor: shape=(), dtype=float32, numpy=0.0>

tf.zeros([1])
Out[91]: <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>

tf.zeros([2, 3])
Out[92]:
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[0., 0., 0.],
       [0., 0., 0.]], dtype=float32)>

tf.zeros([2, 3, 3])
Out[93]:
<tf.Tensor: shape=(2, 3, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],
       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]], dtype=float32)>
  • tf.ones
tf.ones(1)
Out[97]: <tf.Tensor: shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>

tf.ones([])
Out[98]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>

tf.ones([2])
Out[99]: <tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>

tf.ones([2, 3])
Out[100]:
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>
  • tf.fill

填充任意元素的值

tf.fill([2, 2], 0)
Out[102]:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[0, 0],
       [0, 0]])>

tf.fill([2, 2], 0.)
Out[103]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
       [0., 0.]], dtype=float32)>

tf.fill([2, 2], 9)
Out[104]:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[9, 9],
       [9, 9]])>
便捷功能tf.zeros_like、 tf.ones_like

tf.zeros_like(a) == tf.zeros(a.shape)

tf.ones_like(a) == tf.ones_like(a.shape)

  • tf.zeros_like(a)
a = tf.zeros([2, 3, 3])

tf.zeros_like(a)
Out[95]:
<tf.Tensor: shape=(2, 3, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],
       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]], dtype=float32)>

tf.zeros(a.shape)
Out[96]:
<tf.Tensor: shape=(2, 3, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],
       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]], dtype=float32)>
  • tf.ones_like(a)
tf.ones_like(a)
Out[101]:
<tf.Tensor: shape=(2, 3, 3), dtype=float32, numpy=
array([[[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],
       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]], dtype=float32)>
随机化的初始化
  • 正态分布tf.random.normal()

默认均值为0,方差为1的分布

tf.random.normal([2, 2], mean = 1, stddev = 1)
Out[105]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-0.11585343,  1.6131009 ],
       [ 0.7416894 ,  1.796042  ]], dtype=float32)>

tf.random.normal([2, 2])
Out[106]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-0.10901463,  2.072559  ],
       [-0.64495444,  1.5411081 ]], dtype=float32)>
  • 截断的正态分布tf.random.truncated_normal

在原来的数据的基础上截去了某一部分的元素

tf.random.truncated_normal([2, 2], mean = 0, stddev = 1)
Out[107]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-1.4874911 ,  0.585462  ],
       [-0.20348087, -0.25815788]], dtype=float32)>
  • 均匀分布tf.random.uniform()
tf.random.uniform([2, 2], minval = 0, maxval = 1)
Out[108]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0.15056229, 0.8612906 ],
       [0.13073182, 0.3647294 ]], dtype=float32)>

tf.random.uniform([2, 2], minval = 0, maxval = 100)
Out[109]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[32.49322, 99.52206],
       [91.40849, 13.17116]], dtype=float32)>
  • 打散数据tf.random_normal.shuffle()

以下示例是遵循如下假设:

假设a是shape为[10, 784]的数据,b是shape为[10]的数据。

然后将a和b以相同序列打散数据

idx = tf.range(10)
idx = tf.random.shuffle(idx)
idx
Out[113]: <tf.Tensor: shape=(10,), dtype=int32, numpy=array([6, 8, 3, 9, 2, 5, 7, 1, 4, 0])>

a = tf.random.normal([10, 784])
b = tf.random.uniform([10], maxval = 10, dtype = tf.int32)
b
Out[116]: <tf.Tensor: shape=(10,), dtype=int32, numpy=array([8, 1, 4, 2, 1, 7, 0, 0, 0, 7])>

a = tf.gather(a, idx)
b = tf.gather(b, idx)
b
Out[119]: <tf.Tensor: shape=(10,), dtype=int32, numpy=array([0, 0, 2, 7, 4, 7, 0, 1, 1, 8])>
tf.constant
tf.constant(1)
Out[120]: <tf.Tensor: shape=(), dtype=int32, numpy=1>

tf.constant([1])
Out[121]: <tf.Tensor: shape=(1,), dtype=int32, numpy=array([1])>

tf.constant([1, 2.])
Out[122]: <tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>

## 注意,传入数据的维度需要一致
tf.constant([[1, 2.], [3.]])
ValueError: Can't convert non-rectangular Python sequence to Tensor.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值