TensorFlow 張量操作的實(shí)現(xiàn)
TensorFlow 張量操作基礎(chǔ)
張量是TensorFlow中的核心數(shù)據(jù)結(jié)構(gòu),可以理解為多維數(shù)組。張量的秩表示其維度數(shù)量,例如標(biāo)量是0維張量,向量是1維張量,矩陣是2維張量。
import tensorflow as tf # 創(chuàng)建標(biāo)量 scalar = tf.constant(5) # 創(chuàng)建向量 vector = tf.constant([1, 2, 3]) # 創(chuàng)建矩陣 matrix = tf.constant([[1, 2], [3, 4]]) # 創(chuàng)建3維張量 tensor = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
張量創(chuàng)建方法
TensorFlow提供了多種創(chuàng)建張量的方式,包括從Python列表、Numpy數(shù)組創(chuàng)建,以及生成特定模式的張量。
# 從Python列表創(chuàng)建 tensor_from_list = tf.convert_to_tensor([1, 2, 3]) # 從Numpy數(shù)組創(chuàng)建 import numpy as np array = np.array([[1, 2], [3, 4]]) tensor_from_np = tf.convert_to_tensor(array) # 生成全零張量 zeros = tf.zeros([2, 3]) # 生成全一張量 ones = tf.ones([3, 2]) # 生成隨機(jī)正態(tài)分布張量 randn = tf.random.normal([2, 2], mean=0.0, stddev=1.0) # 生成均勻分布張量 randu = tf.random.uniform([3, 3], minval=0, maxval=1)
張量數(shù)學(xué)運(yùn)算
張量支持各種數(shù)學(xué)運(yùn)算,包括逐元素運(yùn)算和矩陣運(yùn)算。
a = tf.constant([[1, 2], [3, 4]]) b = tf.constant([[5, 6], [7, 8]]) # 逐元素加法 add = tf.add(a, b) # 逐元素乘法 mul = tf.multiply(a, b) # 矩陣乘法 matmul = tf.matmul(a, b) # 張量求和 sum_all = tf.reduce_sum(a) sum_axis0 = tf.reduce_sum(a, axis=0) sum_axis1 = tf.reduce_sum(a, axis=1) # 張量平均值 mean = tf.reduce_mean(a) # 張量最大值 max_val = tf.reduce_max(a)
張量形狀操作
改變張量形狀是常見的操作,TensorFlow提供了多種形狀操作方法。
tensor = tf.constant([[1, 2], [3, 4], [5, 6]]) # 獲取張量形狀 shape = tensor.shape # 改變張量形狀 reshaped = tf.reshape(tensor, [2, 3]) # 轉(zhuǎn)置張量 transposed = tf.transpose(tensor) # 擴(kuò)展維度 expanded = tf.expand_dims(tensor, axis=0) # 壓縮維度 squeezed = tf.squeeze(expanded)
張量索引和切片
TensorFlow支持類似Numpy的索引和切片操作。
tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 獲取單個元素 elem = tensor[1, 2] # 獲取第2行第3列的元素 # 獲取行切片 row_slice = tensor[1:, :] # 獲取第2行及以后的所有行 # 獲取列切片 col_slice = tensor[:, 1] # 獲取第2列 # 使用步長切片 strided_slice = tensor[::2, ::2] # 每隔2個元素取一個
張量廣播機(jī)制
TensorFlow支持廣播機(jī)制,允許不同形狀的張量進(jìn)行運(yùn)算。
a = tf.constant([[1, 2, 3]]) # 形狀(1,3) b = tf.constant([[4], [5], [6]]) # 形狀(3,1) # 廣播加法 c = a + b # 結(jié)果形狀(3,3)
張量聚合操作
TensorFlow提供了多種聚合操作函數(shù)。
tensor = tf.constant([[1, 2, 3], [4, 5, 6]]) # 沿軸0求和 sum0 = tf.reduce_sum(tensor, axis=0) # [5,7,9] # 沿軸1求最大值 max1 = tf.reduce_max(tensor, axis=1) # [3,6] # 計(jì)算邏輯與 logical = tf.reduce_all(tensor > 3) # False # 計(jì)算均值 mean = tf.reduce_mean(tensor) # 3.5
張量拼接與分割
TensorFlow支持張量的拼接和分割操作。
a = tf.constant([[1, 2], [3, 4]]) b = tf.constant([[5, 6], [7, 8]]) # 沿軸0拼接 concat0 = tf.concat([a, b], axis=0) # 沿軸1拼接 concat1 = tf.concat([a, b], axis=1) # 張量分割 split0 = tf.split(a, num_or_size_splits=2, axis=0) split1 = tf.split(a, num_or_size_splits=[1, 1], axis=1)
張量排序操作
TensorFlow提供了排序和top-k操作。
tensor = tf.constant([[3, 1, 4], [1, 5, 9]]) # 排序 sorted_values, sorted_indices = tf.sort(tensor, direction='DESCENDING') # argsort argsort = tf.argsort(tensor) # top-k top_k_values, top_k_indices = tf.math.top_k(tensor, k=2)
張量高級操作
TensorFlow還提供了一些高級張量操作。
# 張量收集 tensor = tf.constant([[0, 1, 2], [3, 4, 5]]) indices = tf.constant([0, 1]) gathered = tf.gather(tensor, indices) # 收集第0行和第1行 # 張量分散 updates = tf.constant([10, 20]) scattered = tf.tensor_scatter_nd_update(tensor, [[0, 0], [1, 1]], updates) # 張量條件操作 cond = tf.where(tensor > 3, tensor, tf.zeros_like(tensor)) # 大于3保留原值,否則設(shè)為0
張量梯度計(jì)算
TensorFlow支持自動微分,可以計(jì)算張量操作的梯度。
x = tf.Variable(3.0)
with tf.GradientTape() as tape:
y = x ** 2 + 2 * x + 1
dy_dx = tape.gradient(y, x) # 2x + 2 = 8
張量與Numpy互操作
TensorFlow張量和Numpy數(shù)組可以方便地相互轉(zhuǎn)換。
# 張量轉(zhuǎn)Numpy數(shù)組 tensor = tf.constant([[1, 2], [3, 4]]) numpy_array = tensor.numpy() # Numpy數(shù)組轉(zhuǎn)張量 new_tensor = tf.convert_to_tensor(numpy_array)
到此這篇關(guān)于TensorFlow 張量操作的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)TensorFlow 張量操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
opencv+python實(shí)現(xiàn)圖像矯正
這篇文章主要為大家詳細(xì)介紹了opencv+python實(shí)現(xiàn)圖像矯正,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
python 不以科學(xué)計(jì)數(shù)法輸出的方法
今天小編就為大家分享一篇python 不以科學(xué)計(jì)數(shù)法輸出的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Python遠(yuǎn)程方法調(diào)用實(shí)現(xiàn)過程解析
這篇文章主要介紹了Python遠(yuǎn)程方法調(diào)用實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
PyInstaller無法正確識別.ico文件格式的解決辦法
當(dāng) PyInstaller 無法正確識別你提供的 .ico 文件格式時,盡管文件擴(kuò)展名是 .ico,但內(nèi)容可能不符合要求,本文通過代碼示例給大家介紹詳細(xì)的解決方法,需要的朋友可以參考下2025-07-07

