从零开始学习tensorflow2.0之熟悉tf2.0的数据

导入tensorflow2.0

安装tensorflow2.0,使用pip安装,在jupyter notebook之中
!pip install tensorflow
!pip install tensorflow-gpu

import tensorflow as tf
print(tf.__version__)

2.0.0
2.0.0

tf中的常量

x = tf.constant(range(12))

print(x.shape)
(12,)

查看x的具体数值

x
<tf.Tensor: id=0, shape=(12,), dtype=int32, numpy=array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int32)>

numpy类似的数组查看形状

x.shape
TensorShape([12])

len查看最外层的维度

len(x)
12

reshape函数

X = tf.reshape(x,(3,4))
X
<tf.Tensor: id=2, shape=(3, 4), dtype=int32, numpy=
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]], dtype=int32)>

zeros生成全零

tf.zeros((2,3,4))
<tf.Tensor: id=5, shape=(2, 3, 4), dtype=float32, numpy=
array([[[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]],

       [[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]]], dtype=float32)>

生成全1

tf.ones((3,4))
<tf.Tensor: id=8, shape=(3, 4), dtype=float32, numpy=
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=float32)>

tf的常量函数

Y = tf.constant([[2,1,4,3],[1,2,3,4],[4,3,2,1]])
Y
<tf.Tensor: id=9, shape=(3, 4), dtype=int32, numpy=
array([[2, 1, 4, 3],
       [1, 2, 3, 4],
       [4, 3, 2, 1]], dtype=int32)>

随机数生成normal正太分布
mean均值
stddev标准差

tf.random.normal(shape=[3,4], mean=0, stddev=1)
<tf.Tensor: id=15, shape=(3, 4), dtype=float32, numpy=
array([[ 0.32523802,  0.16670766, -0.9616322 , -2.0908153 ],
       [-0.6289028 , -1.7341328 , -0.5340057 , -0.9329763 ],
       [ 0.3420948 , -0.4493365 , -1.9651859 , -0.3556522 ]],
      dtype=float32)>

按元素相加

X + Y
<tf.Tensor: id=16, shape=(3, 4), dtype=int32, numpy=
array([[ 2,  2,  6,  6],
       [ 5,  7,  9, 11],
       [12, 12, 12, 12]], dtype=int32)>

按元素相乘

X * Y
<tf.Tensor: id=17, shape=(3, 4), dtype=int32, numpy=
array([[ 0,  1,  8,  9],
       [ 4, 10, 18, 28],
       [32, 27, 20, 11]], dtype=int32)>

按元素相除

X / Y
<tf.Tensor: id=20, shape=(3, 4), dtype=float64, numpy=
array([[ 0.  ,  1.  ,  0.5 ,  1.  ],
       [ 4.  ,  2.5 ,  2.  ,  1.75],
       [ 2.  ,  3.  ,  5.  , 11.  ]])>
Y
<tf.Tensor: id=9, shape=(3, 4), dtype=int32, numpy=
array([[2, 1, 4, 3],
       [1, 2, 3, 4],
       [4, 3, 2, 1]], dtype=int32)>

转变数据类型int to float32

Y = tf.cast(Y, tf.float32)

Y
<tf.Tensor: id=21, shape=(3, 4), dtype=float32, numpy=
array([[2., 1., 4., 3.],
       [1., 2., 3., 4.],
       [4., 3., 2., 1.]], dtype=float32)>
Y = tf.cast(Y, tf.float32)
tf.exp(Y)
<tf.Tensor: id=22, shape=(3, 4), dtype=float32, numpy=
array([[ 7.389056 ,  2.7182817, 54.598152 , 20.085537 ],
       [ 2.7182817,  7.389056 , 20.085537 , 54.598152 ],
       [54.59815  , 20.085537 ,  7.389056 ,  2.7182817]], dtype=float32)>

变成int

Y = tf.cast(Y, tf.int32)
tf.matmul(X, tf.transpose(Y))
<tf.Tensor: id=26, shape=(3, 3), dtype=int32, numpy=
array([[ 18,  20,  10],
       [ 58,  60,  50],
       [ 98, 100,  90]], dtype=int32)>

垂直拼接矩阵axis = 0

tf.concat([X,Y],axis = 0)
<tf.Tensor: id=28, shape=(6, 4), dtype=int32, numpy=
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [ 2,  1,  4,  3],
       [ 1,  2,  3,  4],
       [ 4,  3,  2,  1]], dtype=int32)>

水平拼接 axis = 1

tf.concat([X,Y],axis = 1)
<tf.Tensor: id=34, shape=(3, 8), dtype=int32, numpy=
array([[ 0,  1,  2,  3,  2,  1,  4,  3],
       [ 4,  5,  6,  7,  1,  2,  3,  4],
       [ 8,  9, 10, 11,  4,  3,  2,  1]], dtype=int32)>

元素是否相等

tf.equal(X,Y)
<tf.Tensor: id=35, shape=(3, 4), dtype=bool, numpy=
array([[False,  True, False,  True],
       [False, False, False, False],
       [False, False, False, False]])>

所有元素求和

tf.reduce_sum(X)
<tf.Tensor: id=37, shape=(), dtype=int32, numpy=66>

求每个元素的平方和然后开方

X = tf.cast(X, tf.float32)
tf.norm(X)
<tf.Tensor: id=43, shape=(), dtype=float32, numpy=22.494444>

改变形状

A = tf.reshape(tf.constant(range(3)), (3,1))
B = tf.reshape(tf.constant(range(2)), (1,2))
A, B
(<tf.Tensor: id=46, shape=(3, 1), dtype=int32, numpy=
 array([[0],
        [1],
        [2]], dtype=int32)>,
 <tf.Tensor: id=49, shape=(1, 2), dtype=int32, numpy=array([[0, 1]], dtype=int32)>)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值