Hike News
Hike News

深度學習-tensorflow基礎-張量常用操作

生成固定值張量

創建所有元素為零的張量

  • tf.zeros(shape,dtype=tf.float32,name=None)
  • 此操作返回一個具dtype類型,形狀為shape和所有元素設置為零的張量

Example

1
2
3
4
5
6
import tensorflow as tf

zeros = tf.zeros(shape=[2,2],dtype=tf.float32)

with tf.Session() as sess:
print(sess.run(zeros))

Result

1
2
[[0. 0.]
[0. 0.]]

創建與tensor相同shape的零張量

  • tf.zeros_like(tensor,dtype=None,name=None)
  • 返回與tensor參數相同shape的零的張量

Example

1
2
3
4
5
6
7
8
import tensorflow as tf

# tf.constant可用於創建一個常數陣列張量
constant = tf.constant([[1,2,3],[4,5,6]])
zeros = tf.zeros_like(constant)

with tf.Session() as sess:
print(sess.run(zeros))

Result

1
2
[[0 0 0]
[0 0 0]]

創建所有元素為1的張量

  • tf.ones(shape,dtype=tf.float32,name=None)
  • 此操作返回一個具dtype類型,形狀為shape和所有元素設置為1的張量

Example

1
2
3
4
5
6
import tensorflow as tf

ones = tf.ones(shape=[3,3],dtype=tf.float32)

with tf.Session() as sess:
print(sess.run(ones))

Result

1
2
3
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

創建與tensor相同shape的1張量

  • tf.ones_like(tensor,dtype=None,name=None)
  • 返回與tensor參數相同shape的1的張量

Example

1
2
3
4
5
6
7
import tensorflow as tf

constant = tf.constant([[1,2,3,4],[4,5,6,7]])
ones = tf.ones_like(constant)

with tf.Session() as sess:
print(sess.run(ones))

Result

1
2
[[1 1 1 1]
[1 1 1 1]]

創建所有元素為自定義值的張量

  • tf.fill(dims, value, name=None)
  • 返回一個shape為dims,且全部元素為value值的tensor

Example

1
2
3
4
fill = tf.fill([5,5],0.1)

with tf.Session() as sess:
print(sess.run(fill))

Result

1
2
3
4
5
[[0.1 0.1 0.1 0.1 0.1]
[0.1 0.1 0.1 0.1 0.1]
[0.1 0.1 0.1 0.1 0.1]
[0.1 0.1 0.1 0.1 0.1]
[0.1 0.1 0.1 0.1 0.1]]

創建一個常數自定義值張量

  • tf.constant(value, dtype=None, shape=None, name="Const")
  • 返回一個常數張量

Example

1
2
3
4
5
6
import tensorflow as tf

constant = tf.constant([[1,2,3],[4,5,6],[7,8,9]])

with tf.Session() as sess:
print(sess.run(constant))

Result

1
2
3
[[1 2 3]
[4 5 6]
[7 8 9]]

創建隨機張量

為了模擬現實生活中的數據分布狀態

生成隨機值矩陣

  • 高斯分布中輸出隨機值,再從此分布中取出數字組成矩陣並返回
  • tf.random_normal(shape, mean=0.0, stddev=1.0,dtype=float32, seed, name=None)
  • mean:隨機值之平均值
  • stddev:隨機值的標準差

Example

1
2
3
4
5
6
7
import tensorflow as tf

random = tf.random_normal([3,3],mean=175,stddev=3,)
# 創建一個shape為[3,3],平均值為175,標準差為3的的隨機矩陣

with tf.Session() as sess:
print(sess.run(random))

Result

1
2
3
[[176.29504 170.76894 174.0193 ]
[168.04184 171.90128 179.24036]
[170.96518 176.15288 172.40514]]

生成隨機值矩陣_2

  • tf.random_normal()效果一樣,但是所有數字不會超過兩個標準差
  • tf.truncated_normal(shape, mean=0.0, stddev=1.0,dtype=float32, seed, name=None)

tips

normal distribution

  • 標準差越大,則數據越分散;標準差越小,則數據越集中
  • 平均值大小改變波峰的位置

張量變換類型

提供了如下改變張量中數值類型的函數

  • tf.string_to_number(string_tensor,out_type=None,name=None)
    • 將字符串類型轉換成數字類型
  • tf.to_double(x, name='ToDouble')
  • tf.to_float(x, nmae='ToFloat')
  • tf.to_bfloat16(x, name='ToBFloat16')
  • tf.to_int32(x, name='ToInt32')
  • tf.to_int64(x, name='ToInt64')
  • tf.cast(x, dtype, name=None)

    • 萬能任意轉換類型

      1
      2
      3
      4
      5
      6
      7
      8
      import tensorflow as tf
      const = tf.constant([[1,2,3],[4,5,6]])
      print("const:",const)
      new_const = tf.cast(const,dtype=tf.float32)
      print("new_const:",new_const)

      with tf.Session() as sess:
      pass
      1
      2
      const: Tensor("Const:0", shape=(2, 3), dtype=int32)
      new_const: Tensor("Cast:0", shape=(2, 3), dtype=float32)

tensor的擴展

  • 將兩個矩陣按行或列合併
  • tf.concat(values,axis,name='concat')
    • values:接受一個矩陣列表
    • axis:決定按行(0)或列(1)合併

按行(0)合併

axis = 0

1
2
3
4
5
6
a = [[1,2,3],[4,5,6]]
b = [[7,8,9],[10,11,12]]
concat = tf.concat([a,b],axis=0)

with tf.Session() as sess:
print(sess.run(concat))

Result

1
2
3
4
[[ 1  2  3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

按列(1)合併

axis = 1

1
2
3
4
5
6
a = [[1,2,3],[4,5,6]]
b = [[7,8,9],[10,11,12]]
concat = tf.concat([a,b],axis=1)

with tf.Session() as sess:
print(sess.run(concat))

Result

1
2
[[ 1  2  3  7  8  9]
[ 4 5 6 10 11 12]]

提供給tensor運算的數學函數

包括

  • 算數運算符
  • 基本數學函數
  • 矩陣運算
  • 減少維度的運算
  • 序列運算

減少維度的運算(tf.reduce_xxx)

用於減少tensor的維度

tf.reduce_mean

1
2
3
4
x = tf.constant([[1., 1.],[2., 2.]])
tf.reduce_mean(x) # 1.5
tf.reduce_mean(x,0) #[1.5, 1.5] ---->第二個參數為0代表 按column 求值
tf.reduce_mean(x,1) #[1., 2.] ---->第二個參數為1代表 按行 求值

tf.reduce_all

1
2
3
4
x = tf.constant([[True, True],[False, False]])
tf.reduce_all(x) # False ---->對全部求and運算 有一個false即返回false
tf.reduce_all(x,0) #[False, False] ---->第二個參數為0代表 按column 求值
tf.reduce_all(x,1) #[True, False] ---->第二個參數為1代表 按行 求值

參考網址

https://www.tensorflow.org/versions/r1.10/api_docs/python/