1. 程式人生 > 其它 >TF2.1進階-張量合併與拆分-資料統計

TF2.1進階-張量合併與拆分-資料統計

技術標籤:TensorFlow-深度學習numpytensorflow深度學習

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)
2.1.0

1 張量的合併與分割

t1 = tf.constant([[1,2,3],[4,5,6]])
t2 = tf.constant([[7,8,9],[10,11,12]])
t1.ndim,t2.ndim
(2, 2)
# 拼接,必須指定維度
# 和np.concatenate()很像
tf.concat([t1,t2],axis=0)
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])>
# 拼接,必須指定維度
tf.concat([t1,t2],axis=1)
<tf.Tensor: shape=(2, 6), dtype=int32, numpy=
array([[ 1,  2,  3,  7,  8,  9],
       [ 4,  5,  6, 10, 11, 12]])>
# 想把維度疊在一起,不是拼接
img1 = tf.random.uniform([3,4])
plt.matshow(img1)
<matplotlib.image.AxesImage at 0x1d0b9c75c08>

在這裡插入圖片描述

# 想把維度疊在一起,不是拼接
img2 = tf.random.uniform([3,4])
plt.matshow(img2)
<matplotlib.image.AxesImage at 0x1d0ba17e508>

在這裡插入圖片描述

img_stack = tf.stack([img1,img2],axis=0)
# axis指的是在哪個維度前面增加一個新的維度
# 十分注意,只有圖片是相同形狀下才能疊加,否則會出錯
img_stack.shape
TensorShape([2, 3, 4])
# concat 除了要合併的維度,其他的維度的值也是相同的
# stack 所有張量的形狀(維度)都是相同的
# unstack 是stack的逆操作
a,b = tf.unstack(img_stack,axis=0) #在第一維度拆開
a,b
# 注意拆開的值要和變數相對應
(<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
 array([[0.60510254, 0.07157028, 0.8492453 , 0.49489796],
        [0.6152303 , 0.05819559, 0.7963246 , 0.23404086],
        [0.46094084, 0.9648367 , 0.72516274, 0.9123243 ]], dtype=float32)>,
 <tf.Tensor: shape=(3, 4), dtype=float32, numpy=
 array([[0.72922957, 0.7465074 , 0.8792107 , 0.291466  ],
        [0.03773201, 0.26218307, 0.22307646, 0.6719489 ],
        [0.781525  , 0.6514956 , 0.00908875, 0.10414338]], dtype=float32)>)
# [2,3,4]
# [60000,28,28]  60000萬張圖片,28行,28列
# 不完全打散,只是拆分
a,b = tf.split(img_stack,axis=2,num_or_size_splits=2) #平均分兩份 
a,b
(<tf.Tensor: shape=(2, 3, 2), dtype=float32, numpy=
 array([[[0.60510254, 0.07157028],
         [0.6152303 , 0.05819559],
         [0.46094084, 0.9648367 ]],
 
        [[0.72922957, 0.7465074 ],
         [0.03773201, 0.26218307],
         [0.781525  , 0.6514956 ]]], dtype=float32)>,
 <tf.Tensor: shape=(2, 3, 2), dtype=float32, numpy=
 array([[[0.8492453 , 0.49489796],
         [0.7963246 , 0.23404086],
         [0.72516274, 0.9123243 ]],
 
        [[0.8792107 , 0.291466  ],
         [0.22307646, 0.6719489 ],
         [0.00908875, 0.10414338]]], dtype=float32)>)
# 不想平均分成兩份
a,b = tf.split(img_stack,axis=2,num_or_size_splits=[3,1]) #平均分兩份 
a,b
(<tf.Tensor: shape=(2, 3, 3), dtype=float32, numpy=
 array([[[0.60510254, 0.07157028, 0.8492453 ],
         [0.6152303 , 0.05819559, 0.7963246 ],
         [0.46094084, 0.9648367 , 0.72516274]],
 
        [[0.72922957, 0.7465074 , 0.8792107 ],
         [0.03773201, 0.26218307, 0.22307646],
         [0.781525  , 0.6514956 , 0.00908875]]], dtype=float32)>,
 <tf.Tensor: shape=(2, 3, 1), dtype=float32, numpy=
 array([[[0.49489796],
         [0.23404086],
         [0.9123243 ]],
 
        [[0.291466  ],
         [0.6719489 ],
         [0.10414338]]], dtype=float32)>)

2 資料統計

# 向量範數
x = tf.ones([2,2])
x
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],
       [1., 1.]], dtype=float32)>
# 一範數
tf.norm(x,ord=1) 
<tf.Tensor: shape=(), dtype=float32, numpy=4.0>
# 無窮範數
tf.norm(x,ord=np.inf)
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>
# 求最小值
tf.reduce_min(x,axis=0)
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>
# 求最大值
tf.reduce_max(x,axis=0)
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>
# 求均值
tf.reduce_mean(x,axis=0)
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>
# 求和
tf.reduce_sum(x,axis=0)
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>
# 張量比較
a = tf.ones([1],dtype=tf.int32)
b = tf.constant([1,2,1,2,3])
tf.equal(a,b) #注意,a,b資料型別需要一樣!!
<tf.Tensor: shape=(5,), dtype=bool, numpy=array([ True, False,  True, False, False])>
# 將布林變數轉換成0101的形式
z = tf.equal(a,b)
z = tf.cast(z,dtype=tf.int32)
z
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([1, 0, 1, 0, 0])>
#統計有幾個True,將布林變數轉換成0101形式之後進行求和
tf.reduce_sum(z)
<tf.Tensor: shape=(), dtype=int32, numpy=2>