TensorFlow2總結

Tensor數據類型

  • list: [1,1.2,'hello'] ,內存佔用大,處理速度慢
  • np.array,數據同類型,但不支持GPU和自動求導
  • tf.Tensor,爲了彌補numpy的缺點,爲深度學習而生,支持GPU與自動求導
  • tensor:
    • scalar:標量,1.1,dim = 0
    • vector:向量,[1.1],[1.1,2.2,...],dim = 1
    • matrix: 矩陣,[[1.1,2.2],[3.3,4.4]],dim = 2
    • tensor:rank>2 ,dim > 2
  • 數據類型:
    • Int, float, double
    • bool
    • string
  • 定義tensor
tf.constant(1)  # 定義int,普通的tensor
tf.constant(1.)  # 定義float
tf.constant([True, False])  # 定義bool
tf.constant('hello nick')   # 定義string

屬性

with tf.device('cpu'):
  a = tf.constant([1])   # 在cpu創建
with tf.device('gpu'):
    b = tf.constant([1]) # 在gpu創建 
  
a.device # 設備屬性
a.gpu()  # cpu轉gpu
b.cpu()  # gpu轉cpu
a.numpy()# 獲取numpy數據類型
a.shape  # 獲取a的屬性
a.ndim   # 獲取維度
tf.rank(a)  # 獲取維度
a.name  # 1.+歷史遺留問題

數據類型判斷

instance(a,tf.Tensor) # 判斷是否爲tensor (不推薦)
tf.is_tensor(a)  # 判斷是否爲tensor   (推薦)
a.dtype,b.dtype,c.dtype  # 判斷數據類型

數據類型轉換

a = np.arange(5)
aa = tf.convert_to_tensor(a,dtype=tf.int32) # numpy轉tensor

tf.cast(aa,dtype=tf.float32)  # tensor之間數據類型轉換 tf.int32轉tf.float32

b = tf.constant([0,1])
tf.cast(b,dtype=tf.bool) # int轉bool

# tf.Variable
a = tf.range(5)    # 生成一個tensor
b = tf.Variable(a, name='input_data') # tensor轉爲Variable後具有求導的特性,即自動記錄a的梯度相關信息
b.name      # input_data:0
b.trainable # True  # 可訓練

isinstance(b,tf.Tensor)    # False
isinstance(b,tf.Variable)  # True
tf.is_tensor(b)  # True    # 推薦使用

 tensor轉numpy

a= tf.range(5)
a.numpy()

# a必須是scalar
a = tf.ones([])
a.numpy()  
int(a)
float(a)

創建Tensor

from numpy, list
* zeros, ones, fill
* random  # if big dimension, random initial
* constant
* Application

numpy, list

numpy

import numpy as np
import tensorflow as tf

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

list

tf.convert_to_tensor([1, 2])
<tf.Tensor: id=4, shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>
tf.convert_to_tensor([1, 2.])
<tf.Tensor: id=6, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>
tf.convert_to_tensor([[1], [2.]])
<tf.Tensor: id=8, shape=(2, 1), dtype=float32, numpy=
array([[1.],
       [2.]], dtype=float32)>

zeros, ones, fill

zeros

tf.zeros([])
<tf.Tensor: id=10, shape=(), dtype=float32, numpy=0.0>
tf.zeros([1])
<tf.Tensor: id=14, shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>
tf.zeros([2, 2])
<tf.Tensor: id=18, shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
       [0., 0.]], dtype=float32)>
tf.zeros([2, 3, 3])
<tf.Tensor: id=22, 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)>
a = tf.constant([0])
tf.zeros_like(a)  # 等同於tf.zeros(a.shape)
<tf.Tensor: id=25, shape=(1,), dtype=int32, numpy=array([0], dtype=int32)>

ones

 

tf.ones(1)
<tf.Tensor: id=29, shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>
tf.ones([])
<tf.Tensor: id=31, shape=(), dtype=float32, numpy=1.0>
tf.ones([2])
<tf.Tensor: id=35, shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>
tf.ones([2, 3])
<tf.Tensor: id=39, shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>
a = tf.constant([0])
tf.ones_like(a)  # # 等同於tf.ones(a.shape)
<tf.Tensor: id=44, shape=(1,), dtype=int32, numpy=array([1], dtype=int32)>

fill

 

tf.fill([2, 2], 0)
<tf.Tensor: id=48, shape=(2, 2), dtype=int32, numpy=
array([[0, 0],
       [0, 0]], dtype=int32)>
tf.fill([2, 2], 0)
<tf.Tensor: id=52, shape=(2, 2), dtype=int32, numpy=
array([[0, 0],
       [0, 0]], dtype=int32)>
tf.fill([2, 2], 1)
<tf.Tensor: id=56, shape=(2, 2), dtype=int32, numpy=
array([[1, 1],
       [1, 1]], dtype=int32)>
tf.fill([2, 2], 9)
<tf.Tensor: id=60, shape=(2, 2), dtype=int32, numpy=
array([[9, 9],
       [9, 9]], dtype=int32)>

random

 

# 正態分佈,均值爲1,方差爲1
tf.random.normal([2, 2], mean=1, stddev=1)
<tf.Tensor: id=67, shape=(2, 2), dtype=float32, numpy=
array([[1.0804566, 0.9318387],
       [1.0620257, 0.6907253]], dtype=float32)>
tf.random.normal([2, 2])
<tf.Tensor: id=74, shape=(2, 2), dtype=float32, numpy=
array([[-0.06452972,  0.05704789],
       [ 0.82857376,  0.71619517]], dtype=float32)>
# 截斷的正態分佈,
tf.random.truncated_normal([2, 2], mean=0, stddev=1)
<tf.Tensor: id=81, shape=(2, 2), dtype=float32, numpy=
array([[ 0.19161457, -0.820383  ],
       [ 0.43668088, -0.3798696 ]], dtype=float32)>

如下圖所示爲截斷正態分佈,截掉紅色部分,對新的正態分佈重新採樣。因爲sigmoid的和新的正態分佈不衝突的地方的區域,對於sigmoid函數來說是近似於平滑的直線,梯度爲0,因此會有梯度消失。

 

# 均勻分佈
tf.random.uniform([2, 2], minval=0, maxval=1)
<tf.Tensor: id=89, shape=(2, 2), dtype=float32, numpy=
array([[0.01481438, 0.15071952],
       [0.5599004 , 0.59821343]], dtype=float32)>
tf.random.uniform([2, 2], minval=0, maxval=100, dtype=tf.int32)
<tf.Tensor: id=102, shape=(2, 2), dtype=int32, numpy=
array([[51,  9],
       [10, 14]], dtype=int32)>

打亂idx後,a和b的索引不變

idx = tf.range(10)
idx = tf.random.shuffle(idx)
idx
<tf.Tensor: id=113, shape=(10,), dtype=int32, numpy=array([0, 8, 4, 9, 6, 7, 5, 2, 1, 3], dtype=int32)>
a = tf.random.normal([10, 784])
b = tf.random.uniform([10], maxval=10, dtype=tf.int32)
b
<tf.Tensor: id=134, shape=(10,), dtype=int32, numpy=array([1, 8, 1, 2, 4, 6, 2, 7, 4, 5], dtype=int32)>
a = tf.gather(a, idx)
b = tf.gather(b, idx)
b
<tf.Tensor: id=147, shape=(10,), dtype=int32, numpy=array([1, 8, 2, 2, 6, 1, 7, 4, 4, 5], dtype=int32)>

constant

tf.constant(1)
<tf.Tensor: id=149, shape=(), dtype=int32, numpy=1>
tf.constant([1])
<tf.Tensor: id=151, shape=(1,), dtype=int32, numpy=array([1], dtype=int32)>
tf.constant([1, 2.])
<tf.Tensor: id=153, shape=(2,), dtype=float32, numpy=array([1., 2.], dtype=float32)>
tf.constant([[1, 2], [3., 2]])
<tf.Tensor: id=156, shape=(2, 2), dtype=float32, numpy=
array([[1., 2.],
       [3., 2.]], dtype=float32)>

loss計算

無bias的loss

out = tf.random.uniform([4, 10])
out
<tf.Tensor: id=171, shape=(4, 10), dtype=float32, numpy=
array([[0.67733276, 0.2267617 , 0.21761227, 0.28679788, 0.68864655,
        0.21349418, 0.5646602 , 0.8294822 , 0.22094071, 0.20246148],
       [0.7940483 , 0.86402774, 0.78399694, 0.80085063, 0.01357341,
        0.11889946, 0.89162886, 0.755934  , 0.8058628 , 0.40188062],
       [0.115659  , 0.30951428, 0.39866602, 0.5358803 , 0.9163326 ,
        0.47557557, 0.9397205 , 0.3110628 , 0.49839914, 0.34321547],
       [0.5563061 , 0.78829396, 0.52705276, 0.29077685, 0.35033226,
        0.9630101 , 0.338771  , 0.6301584 , 0.7393383 , 0.7073529 ]],
      dtype=float32)>
y = tf.range(4)
y = tf.one_hot(y, depth=10)
y
<tf.Tensor: id=188, shape=(4, 10), dtype=float32, numpy=
array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]], dtype=float32)>
loss = tf.keras.losses.mse(y, out)
loss
<tf.Tensor: id=195, shape=(4,), dtype=float32, numpy=array([0.19016443, 0.4096697 , 0.31698173, 0.43206215], dtype=float32)>
loss = tf.reduce_mean(loss)
loss
<tf.Tensor: id=200, shape=(), dtype=float32, numpy=0.3372195>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章