04. TensorFlow2.0的基础知识

1. 数据类型

1.1. 数据容器

  • list
  • np.array
  • tf.Tensor

1.2. What’s Tensor

  • scalar: 1.1
  • vector: [1.1],[1.1, 2.2, … ]
  • matrix: [[1.1, 2.2],[3.3, 4.4],[5.5, 6.6]]
  • tensor: 𝑟𝑎𝑛𝑘 > 2 (在神经网络中, tensor的范围相当广, 这里可以理解为狭义的定义)

1.3. 数据类型(TF is a computing lib)

  • int, float, double
  • bool
  • string

1.3.1. Create

  • tf.constant(1)
    • <tf.Tensor: id=0, shape=(), dtype=int32, numpy=1>
  • tf.constant(1.0)
    • <tf.Tensor: id=1, shape=(), dtype=float32, numpy=1.0>
  • tf.constant(2.2, dtype=tf.int32)
    • error:TypeError: Cannot convert 2.2 to EagerTensor of dtype int32
  • tf.constant(2., dtype=tf.double) (注意:tf.double 实际上就是tf.float64的别名)
    • <tf.Tensor: id=3, shape=(), dtype=float64, numpy=2.0>
  • tf.constant([True, False])
    • <tf.Tensor: id=4, shape=(2,), dtype=bool, numpy=array([ True, False])>
  • tf.constant(“hello world”)
    • <tf.Tensor: id=5, shape=(), dtype=string, numpy=b’hello world’>

1.3.2. Tensor Property

在这里插入图片描述

1.3.3. Check Tensor Type

import tensorflow as tf
import numpy as np


a = tf.constant([1.])
b = tf.constant([True, False])
c = tf.constant('hello world')
d = np.arange(4)

print(isinstance(a, tf.Tensor))  # 注意: tf.Variable也是tensor,但是此时就不能用isinstance(,tf.Tensor)来判断了
print(tf.is_tensor(b))
print(tf.is_tensor(d))  # 是一个numpy,而非tensor
print(a.dtype, b.dtype, c.dtype)
print(a.dtype == tf.float32)

1.3.4. Convert

在这里插入图片描述

1.3.5. bool & int

在这里插入图片描述

1.3.6. tf.Variable

在这里插入图片描述

1.3.7. To numpy

在这里插入图片描述

2. 创建Tensor

2.1. Outline

  • from numpy, list
  • zeros, ones
  • fill
  • random
  • constant
  • Application

2.1.1. From Numpy, List

在这里插入图片描述

2.1.2. zeros, ones

  • tf.zeros

    • tf.ones类似
      在这里插入图片描述
  • tf.zeros_like

    • tf.ones_like

在这里插入图片描述

2.1.3. Fill

在这里插入图片描述

2.1.4. Normal 正态分布

在这里插入图片描述

  • Truncated Distribution 截断分布
    • Gradient Vanish 截断消失
      • 链接:https://assessingpsyche.wordpress.com/2014/06/04/using-the-truncated-normal-distribution/

2.1.5. Uniform 均匀分布

  • tf.random.uniform([2, 2], minval=0,maxval=1)

  • tf.random.uniform([2,2], minval=0, maxval=100)

  • Random Permutation 随机置换
    在这里插入图片描述

2.2. Typical Dim Data

2.2.1. Scalar

  • []
    • 0, 1., 2.2…
    • loss = mse(out, y)
      在这里插入图片描述
    • accuracy

2.2.2. Vector

在这里插入图片描述
在这里插入图片描述

2.2.3. Matrix

在这里插入图片描述
###.2.3.4. Dim=3 Tensor (自然语言处理)
在这里插入图片描述
在这里插入图片描述

2.3.5. Dim=4 Tensor

  • Image: [b, h, w, 3]
  • feature maps: [b, h, w, c]
    在这里插入图片描述

2.3.6. Dim=5 Tensor

在这里插入图片描述

3. 需要全套课程视频+PPT+代码资源可以私聊我

  • 方式1:CSDN私信我!
  • 方式2:QQ邮箱:594042358@qq.com或者直接加我QQ: 594042358!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值