目錄
第1章 Tensor運算概述
1.1 概述
1.3 “in place“運算
1.4 Tensor的廣播機制
1.5 環境準備
1.6 算術運算概述:加、減、系數乘、系數除
第2章 加法運算:add
第3章 減法運算:subtract
第4章 乘法運算:multiply
第5章除法運算:divide
第6章 倒數運算 /
第7章 幂運算:power()
第8章 廣播機制
TensorFlow提供了大量的張量運算,基本上可以對标Numpy多元數組的運算,以支援對張量的各種複雜的運算。
這些操作運算中大多是對數組中每個元素執行相同的函數運算,并獲得每個元素函數運算的結果序列,這些序列生成一個新的同次元的數組。
https://tensorflow.google.cn/tutorials/quickstart/beginner?hl=zh-cn
1.2 運算分類
(1)算術運算:加、減、系數乘、系數除
(2)函數運算:sin,cos
(3)取整運算:上取整、下取整
(4)統計運算:最大值、最小值、均值
(5)線性代數運算
NA
實作不同次元tensor的擴充運算
#環境準備
import numpy as np
import tensorflow as tf
print("hello world")
print("tensorflow version:", tf.__version__)
Tensor算術函數包含簡單的加減乘除運算: add(),subtract(),multiply() 和 divide()。
算術運算後得到的結果依然是是相同次元的數組。
#代碼執行個體
a = tf.range(9)
print ('原資料a:')
print (a)
print ('\n')
b = tf.range(9)
print ('原資料b:')
print (b)
print ('\n')
print ('運算後資料:')
print (a+b)
print (tf.add(a,b))
#print (a.add(b)) #不支援
print ('\n')
print (a)
print ('\n')
輸出結果為:
原資料a:
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)
原資料b:
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)
運算後資料:
tf.Tensor([ 0 2 4 6 8 10 12 14 16], shape=(9,), dtype=int32)
tf.Tensor([ 0 2 4 6 8 10 12 14 16], shape=(9,), dtype=int32)
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)
#代碼執行個體
a = tf.range(1,10)
print ('原資料a:')
print (a)
print ('\n')
b = tf.range(0,9)
print ('原資料b:')
print (b)
print ('\n')
print ('運算後資料:')
print (a-b)
print (tf.subtract(a,b))
#print (a.subtract(b)) 不支援
print ('\n')
print (a)
print ('\n')
#輸出結果
原資料a:
tf.Tensor([1 2 3 4 5 6 7 8 9], shape=(9,), dtype=int32)
原資料b:
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)
運算後資料:
tf.Tensor([1 1 1 1 1 1 1 1 1], shape=(9,), dtype=int32)
tf.Tensor([1 1 1 1 1 1 1 1 1], shape=(9,), dtype=int32)
tf.Tensor([1 2 3 4 5 6 7 8 9], shape=(9,), dtype=int32)
#代碼執行個體
a = tf.constant([2,4,6,8,10,12])
print ('原資料a:')
print (a)
print ('\n')
b = tf.constant([2,4,6,8,10,12])
print ('原資料b:')
print (b)
print ('\n')
print ('運算後資料:')
print (a*b)
print (tf.multiply(a,b))
# print (a.multiply(b)) #不支援
print ('\n')
print (a)
print ('\n')
#輸出結果
原資料a:
tf.Tensor([ 2 4 6 8 10 12], shape=(6,), dtype=int32)
原資料b:
tf.Tensor([ 2 4 6 8 10 12], shape=(6,), dtype=int32)
運算後資料:
tf.Tensor([ 4 16 36 64 100 144], shape=(6,), dtype=int32)
tf.Tensor([ 4 16 36 64 100 144], shape=(6,), dtype=int32)
tf.Tensor([ 2 4 6 8 10 12], shape=(6,), dtype=int32)
#代碼執行個體
a = tf.constant([2,4,6,8,10,12])
print ('原資料a:')
print (a)
print ('\n')
b = tf.constant([2,4,6,8,10,12])
print ('原資料b:')
print (b)
print ('\n')
print ('運算後資料:')
print (a/b)
print (tf.divide(a,b))
# print (a.divide(b)) # 不支援
print ('\n')
print (a)
print ('\n')
#輸出結果
原資料a:
tf.Tensor([ 2 4 6 8 10 12], shape=(6,), dtype=int32)
原資料b:
tf.Tensor([ 2 4 6 8 10 12], shape=(6,), dtype=int32)
運算後資料:
tf.Tensor([1. 1. 1. 1. 1. 1.], shape=(6,), dtype=float64)
tf.Tensor([1. 1. 1. 1. 1. 1.], shape=(6,), dtype=float64)
tf.Tensor([ 2 4 6 8 10 12], shape=(6,), dtype=int32)
reciprocal() 函數傳回參數逐元素的倒數。如 1/4 倒數為 4/1。
#執行個體
a = tf.constant([2,4,5,8,10,20])
print("原資料a")
print(a)
#導數運算
print("運算資料")
print(1/a)
# print(tf.reciprocal(a)) #不支援
# print(a.reciprocal()) # 不支援
print("原資料")
print (a)
#輸出結果為:
原資料a
tf.Tensor([ 2 4 5 8 10 20], shape=(6,), dtype=int32)
運算資料
tf.Tensor([0.5 0.25 0.2 0.125 0.1 0.05 ], shape=(6,), dtype=float64)
原資料
tf.Tensor([ 2 4 5 8 10 20], shape=(6,), dtype=int32)
power() 函數将第一個輸入數組中的元素作為底數,計算它與第二個輸入數組中相應元素的幂。
#執行個體
a = tf.constant([10,10,10,10,10])
b = tf.constant([1,2,3,4,5])
print("原資料a")
print(a)
print("原資料b")
print(b)
#導數運算
print("運算後資料")
#print(a^b)
print(tf.pow(a,b))
# print(a.pow(b)) #不支援
print("原資料a")
print (a)
#輸出結果為:
原資料a
tf.Tensor([10 10 10 10 10], shape=(5,), dtype=int32)
原資料b
tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)
運算後資料
tf.Tensor([ 10 100 1000 10000 100000], shape=(5,), dtype=int32)
原資料a
tf.Tensor([10 10 10 10 10], shape=(5,), dtype=int32)
a = tf.range(9)
print ('原資料:')
print (a)
b = tf.reshape(a, [3,3])
print ('\n第一個數組:')
print (b)
print ('\n第二個數組:')
c = np.array([10,10,10])
print (c)
print ('\n兩個數組相加:')
print (np.add(b,c))
輸出結果:
原資料:
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)
第一個數組:
tf.Tensor(
[[0 1 2]
[3 4 5]
[6 7 8]], shape=(3, 3), dtype=int32)
第二個數組:
[10 10 10]
兩個數組相加:
[[10 11 12]
[13 14 15]
[16 17 18]]