1. 程式人生 > 實用技巧 >深度學習(機器學習)tensorflow學習第二課——建立tensor

深度學習(機器學習)tensorflow學習第二課——建立tensor

建立Tensor:

1、From numpy,list CREATE tensor:

import os

os.environ
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2"
import tensorflow as tf
import numpy as np

# From numpy,list CREATE tensor
a = tf.convert_to_tensor(np.zeros([2, 3]))  # 建立一個兩行三列的0矩陣
# tf.Tensor(
# [[0. 0. 0.]
#  [0. 0. 0.]], shape=(2, 3), dtype=float64)
print(a) a1 = tf.convert_to_tensor(np.ones([2, 3])) # 建立一個兩行三列的1矩陣 # tf.Tensor( # [[1. 1. 1.] # [1. 1. 1.]], shape=(2, 3), dtype=float64) print(a1) a2 = tf.convert_to_tensor([1, 2]) # 直接傳入一個list 也可以 print(a2) # tf.Tensor([1 2], shape=(2,), dtype=int32) a3 = tf.convert_to_tensor([1, 2.]) # 如果前面的是int,後面是float,則會依照後面的型別
print(a3) # tf.Tensor([1. 2.], shape=(2,), dtype=float32) a4=tf.convert_to_tensor([[1],[2]]) # tf.Tensor( # [[1] # [2]], shape=(2, 1), dtype=int32) print(a4)

2、tf.zeros 建立tensor:

"""
tf.zeros 建立tensor
zeros()就是
建立一個所有元素設定為零的張量。
這個操作返回一個型別為' dtype '的張量,其中shape為' shape '和
所有元素設定為零。
zeros()傳入的都是shape

shape=()是什麼意思?
簡單的說舉個例子,[[4,5,6],
                  [7,8,9]]
首先我們把最外面的大括號拿掉,就剩下[4,5,6],[7,8,9],就剩下了2塊
在拿下一個[],就剩下了3個數字,所以shape(2,3)
""" b = tf.zeros([]) print(b) # tf.Tensor(0.0, shape=(), dtype=float32) b1 = tf.zeros([1]) print(b1) # tf.Tensor([0.], shape=(1,), dtype=float32) b2 = tf.zeros([2, 2]) # tf.Tensor( # [[0. 0.] # [0. 0.]], shape=(2, 2), dtype=float32) print(b2) b3 = tf.zeros([2, 3, 3]) # tf.Tensor( # [[[0. 0. 0.] # [0. 0. 0.] # [0. 0. 0.]] # # [[0. 0. 0.] # [0. 0. 0.] # [0. 0. 0.]]], shape=(2, 3, 3), dtype=float32) print(b3) b4 = tf.zeros_like(b3) # 複製 # tf.Tensor( # [[[0. 0. 0.] # [0. 0. 0.] # [0. 0. 0.]] # # [[0. 0. 0.] # [0. 0. 0.] # [0. 0. 0.]]], shape=(2, 3, 3), dtype=float32) print(b4)

3、tf.ones 建立tensor:

"""
tf.ones 建立tensor
ones()就是
建立一個所有元素設定為1的張量。
這個操作返回一個型別為' dtype '的張量,其中shape為' shape '和
所有元素設定為零。
ones()傳入的也都是shape

不管是zeros()或者ones() 方法,主要用於後期演算法中,初始化全為0或者初始化全為1的時候
"""
c = tf.ones(1)
print(c)#tf.Tensor([1.], shape=(1,), dtype=float32)

c1=tf.ones([1])
print(c1)#tf.Tensor([1.], shape=(1,), dtype=float32)

c2=tf.ones([2,2])
# tf.Tensor(
# [[1. 1.]
#  [1. 1.]], shape=(2, 2), dtype=float32)
# print(c2)

c3=tf.ones([2,3,3])
#tf.Tensor(
# [[[1. 1. 1.]
#   [1. 1. 1.]
#   [1. 1. 1.]]
#
#  [[1. 1. 1.]
#   [1. 1. 1.]
#   [1. 1. 1.]]], shape=(2, 3, 3), dtype=float32)
print(c3)

c4=tf.ones_like(c3)
# tf.Tensor(
# [[[1. 1. 1.]
#   [1. 1. 1.]
#   [1. 1. 1.]]
#
#  [[1. 1. 1.]
#   [1. 1. 1.]
#   [1. 1. 1.]]], shape=(2, 3, 3), dtype=float32)
print(c3)

4、tf.fill 建立tensor:

"""
不管是zeros()或者ones() 方法,主要用於後期演算法中,初始化全為0或者初始化全為1的時候
但是如果既不想初始化全為0,又不想初始化全為1的話,我們可以用fill()方法
fill(shape,填充的數字)
"""

d=tf.fill([2,2],1)
# tf.Tensor(
# [[1 1]
#  [1 1]], shape=(2, 2), dtype=int32)
print(d)

d1=tf.fill([2,2],9)
# tf.Tensor(
# [[9 9]
#  [9 9]], shape=(2, 2), dtype=int32)
print(d1)

d2=tf.fill([2,3,3],6)
# tf.Tensor(
# [[[6 6 6]
#   [6 6 6]
#   [6 6 6]]
#
#  [[6 6 6]
#   [6 6 6]
#   [6 6 6]]], shape=(2, 3, 3), dtype=int32)
print(d2)

4、tf.random.normal

"""
隨機化初始化,最常用的tf.random.normal
正態分佈初始化

"""

f=tf.random.normal([2,2],mean=1,stddev=1) # mean=均線,stddev=方差
# tf.Tensor(
# [[ 0.8271891   0.49986267]
#  [-0.55206406 -0.30683088]], shape=(2, 2), dtype=float32)
print(f)

f1=tf.random.truncated_normal([2,2],mean=0,stddev=1) # 將正態分佈的面積擷取一塊之後再初始化
# tf.Tensor(
# [[ 1.3095937 -0.4562364]
#  [-1.0924749 -0.5573949]], shape=(2, 2), dtype=float32)
print(f1)

5、tf.random.uniform

"""
均勻分佈uniform:從minval=0,maxval=1,就是從0-1均勻取樣,形成初始化shape
還可以選擇型別
"""

g=tf.random.uniform([2,2],minval=0,maxval=1)
# tf.Tensor(
# [[0.00387156 0.95663893]
#  [0.41450417 0.864311  ]], shape=(2, 2), dtype=float32)
print(g)

g1=tf.random.uniform([2,2],minval=0,maxval=100,dtype=tf.int32)
# tf.Tensor(
# [[55 70]
#  [ 8 46]], shape=(2, 2), dtype=int32)
print(g1)

6、小小應用————打散資料

"""
小小應用————打散資料
"""

idx = tf.range(10)  # 建立0-9的資料
print(idx)  # tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)

_idx = tf.random.shuffle(idx)  # 打散 資料
print(_idx)  # tf.Tensor([7 4 9 1 8 3 2 0 5 6], shape=(10,), dtype=int32)

b = tf.random.uniform([10], maxval=10, dtype=tf.int32)
print("random_uniform:", b)  # random_uniform: tf.Tensor([9 2 2 9 9 7 1 1 9 7], shape=(10,), dtype=int32)

_b = tf.gather(b, _idx)
print("對應打散的b:", _b)  # 對應b: tf.Tensor([2 9 9 9 2 1 9 1 7 7], shape=(10,), dtype=int32)