Tensorflow2基础总结

1 数据类型

1.1 数值类型

标量:单个实数,shape=[]

向量:1维实数集合,shape=[n]

矩阵:n行m列集合,shape=[n,m]

张量:维度dim>2的数组

Tensorflow一般把以上统称为张量

a = tf.constant([0,1,2,3,4],dtype=tf.int32)
Out: <tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4])>

此处shape=[4,]表示创建的是向量

   a.numpy()
   Out: array([1, 2, 3, 4])

1.2 字符类型用的少不再赘述

1.3 布尔类型

   a = tf.constant(True)
   out: <tf.Tensor: shape=(), dtype=bool, numpy=True>

2 数值精度

一般使用tf.int32、tf.int64、tf.float32、tf.float64在创建张量时定义

   a.dtype#读取精度
   tf.cast(a,tf.int64)#精度转换

精度转换时注意发生数据溢出(高精度转低精度时)

   a = tf.constant(123456789,dtype = tf.int32)
   Out[10]: <tf.Tensor: shape=(), dtype=int32, numpy=123456789>
           
   tf.cast(a,tf.int16)
   Out[12]: <tf.Tensor: shape=(), dtype=int16, numpy=-13035>

布尔值与整数之间的转换也是合法的

   In [16]: a = tf.constant(123456789,dtype = tf.int32)
   In [17]: tf.cast(a,tf.bool)
   
   Out[17]: <tf.Tensor: shape=(), dtype=bool, numpy=True>

0为False,非0数字为True

3 待优化张量

例如参数w1 b1 w2 b2…需要计算计算梯度信息的张量,叫做待优化张量

   a = tf.Variable([1,2,3])#也可以将普通张量转化为待优化张量

4 创建张量

4.1从数组、列表对象创建

将Numpy数组或python列表转化为Tensor张量

   tf.convert_to_tensor([1,2.])
   tf.convert_to_tensor(np.array([[1,2],[3,4]]))

4.2创建全0或全1的张量

   tf.zeros([2,2])#shape=[2,2]
   
   tf.ones([3,3,3])#shape=[3,3,3]

创建形如a的全0\1的张量

   tf.ones_like(a)

4.3创建自定义数值的张量

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

4.4创建已知分布的张量

   tf.random.normal([2,2],meam=0,stddev=1)
   #创建正态分布的张量,shape=[2,2],均值为0,标准差为1
   tf.random.uniform([3,3],minval=0,maxval=1)
   #创建均匀分布张量,区间为[0,1]----如果没有规定范围默认0-1

4.5创建序列

   tf.range()#python的range用法一致

5.维度变换

5.1视图变换

需要理解存储、视图者之间的关系:同一存储,从不同角度观察数据能得到不同的视图。

改变视图的前提是存储不需要改变,否则改变视图操作是不合法的。
在这里插入图片描述
如果定义不合法的视图,例如将[b,h,w,c]转换为[b,c,h,w]呢?这个时候存储就进行了改变。

需要交换维度!

   a = tf.random.normal([2,32,32,6])
   b=tf.transpose(a,perm=[3,1,2,0])
   
   In [28]: b.shape
   Out[28]: TensorShape([6, 32, 32, 2])

5.2.增删维度

增加维度

   In [29]: a = tf.random.normal([2,32,32,6])
   In [30]: b = tf.expand_dims(a,axis=0)
   In [31]: b.shape
       
   Out[31]: TensorShape([1, 2, 32, 32, 6])

删除维度(只能删除维度为1的)

   In [29]: b = tf.random.normal([1,2,32,32,6])
   In [37]: c = tf.squeeze(b,axis=2)
   #error
   out: Can not squeeze dim[2], expected a dimension of 1
   
   In [38]: c = tf.squeeze(b,axis=0)
   In [40]: c.shape
   Out[40]: TensorShape([2, 32, 32, 6])

6 复制

   In [44]: a = tf.constant([[1,2,4],[3,4,6]])
   In [47]: a.numpy()
   Out[47]:
   array([[1, 2, 4],
          [3, 4, 6]])
   
   In [48]: tf.tile(a,multiples=[2,1])#即在axit=0位置复制1次,在axis=1位置不复制
   Out[48]:
   <tf.Tensor: shape=(4, 3), dtype=int32, numpy=
   array([[1, 2, 4],
          [3, 4, 6],
          [1, 2, 4],
          [3, 4, 6]])>

tip:x@w+b中,b的加法运算会自动进行插入和复制数据的操作,不需要手动执行。(广播机制)

   In [54]: a
   Out[54]:
   <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
   array([[1, 2, 4],
          [3, 4, 6]])>
          
   In [52]: c
   Out[52]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
   
   In [55]: a+c
   Out[55]:
   <tf.Tensor: shape=(2, 3), dtype=int32, numpy=
   array([[2, 3, 5],
          [4, 5, 7]])>

在这里插入图片描述

7 数学运算

7.1加减乘除、整除、余除(略)

7.2乘方运算

可以使用tf.pow(x,n),也可以使用x**n的方法。

   In [20]: a
   Out[20]:
   <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
   array([[7, 7],
          [3, 9],
          [8, 5]])>
   
   In [21]: a = tf.pow(a,2)
   Out[21]:
   <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
   array([[49, 49],
          [ 9, 81],
          [64, 25]])>

特别的,平方运算tf.square(x),平方根运算tf.sqrt(x)。

7.3指数运算和对数运算

   tf.exp(1.)#自然指数
   tf.matb,log(x)#对数运算

如希望计算其他底数的对数,可以使用换底公式
在这里插入图片描述

8 矩阵乘法

矩阵乘法的规则与线性代数一致,这里要求:A的第一维度长度和B的倒数第二个维度长度相等。

      a = tf.random.uniform ([4,3,28,32])
      b = tf.random.uniform ([4,3,32,2])
      c=a@b
      c.shape
      out: TensorShape([4, 3, 28, 2])

矩阵乘法支持自动扩展。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值