最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TensorFlow 張量操作的實(shí)現(xiàn)

 更新時間:2025年08月20日 08:50:13   作者:usaccn  
本文介紹了TensorFlow中的張量基礎(chǔ)操作,涵蓋張量創(chuàng)建、數(shù)學(xué)運(yùn)算、形狀操作、索引切片、廣播機(jī)制、聚合操作、排序及高級操作等核心內(nèi)容,具有一定的參考價值,感興趣的可以了解一下

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]

# 計算邏輯與
logical = tf.reduce_all(tensor > 3)  # False

# 計算均值
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

張量梯度計算

TensorFlow支持自動微分,可以計算張量操作的梯度。

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)文章

  • python pip如何手動安裝二進(jìn)制包

    python pip如何手動安裝二進(jìn)制包

    這篇文章主要介紹了python pip如何手動安裝二進(jìn)制包,幫助大家更好的進(jìn)行python開發(fā),感興趣的朋友可以了解下
    2020-09-09
  • 基于Python實(shí)現(xiàn)在線二維碼生成工具

    基于Python實(shí)現(xiàn)在線二維碼生成工具

    這篇文章將為大家展示如何通過純Python編程的方式,開發(fā)出一個網(wǎng)頁應(yīng)用—基于輸入的網(wǎng)址等文字內(nèi)容實(shí)現(xiàn)二維碼的生成,感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • 最新評論

    高要市| 临泽县| 遵化市| 丹棱县| 永清县| 崇明县| 孟津县| 南丰县| 井冈山市| 淳化县| 鸡东县| 吉首市| 柳江县| 札达县| 彭阳县| 乌鲁木齐市| 红安县| 浦北县| 乌兰浩特市| 星座| 孙吴县| 洞头县| 闻喜县| 荔波县| 白河县| 武宣县| 溆浦县| 贡山| 湘潭县| 华安县| 玛曲县| 云阳县| 原阳县| 鄂尔多斯市| 太和县| 石家庄市| 江北区| 梁山县| 灵宝市| 合肥市| 庆元县|