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

TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換

 更新時間:2021年09月08日 10:54:06   作者:我是小白呀  
這篇文章主要介紹了TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

張量排序

在這里插入圖片描述

tf.sort

tf.sort函數(shù)可以幫我們對張量進行排序.

格式:

tf.sort(
    values, axis=-1, direction='ASCENDING', name=None
)

參數(shù):

  • values: 要進行排序的張量
  • axis: 操作維度
  • direction: 正序或者倒序
  • name: 數(shù)據(jù)名稱

例子:

# 創(chuàng)建張量0~9, 并打亂順序
a = tf.random.shuffle(tf.range(10))
print(a)

# 從小到大
b = tf.sort(a)  # direction="ASCENDING"
print(b)

# 從大到小
c = tf.sort(a, direction="DESCENDING")
print(c)

輸出結(jié)果:

tf.Tensor([6 3 7 5 4 0 2 9 8 1], shape=(10,), dtype=int32)
tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
tf.Tensor([9 8 7 6 5 4 3 2 1 0], shape=(10,), dtype=int32)

tf.argsort

tf.argsort返回張量的索引排序, 沿給的軸排序.

格式:

tf.argsort(
    values, axis=-1, direction='ASCENDING', stable=False, name=None
)

參數(shù):

  • 要進行排序的張量
  • axis: 操作維度
  • direction: 正序或者倒序
  • stable: 如果為 True, 則原始張量中的相等元素將不會按返回的順序重新排序
  • name: 數(shù)據(jù)名稱

例子:

# 創(chuàng)建張量0~9, 并打亂順序
a = tf.random.shuffle(tf.range(10))
print(a)

# 從小到大
b = tf.argsort (a)
print(b)

# 從大到小
c = tf.argsort (a, direction="DESCENDING")
print(c)

輸出結(jié)果:

tf.Tensor([9 4 3 1 2 6 0 5 7 8], shape=(10,), dtype=int32)
tf.Tensor([6 3 4 2 1 7 5 8 9 0], shape=(10,), dtype=int32)
tf.Tensor([0 9 8 5 7 1 2 4 3 6], shape=(10,), dtype=int32)

tf.math.top_k

tf.math.top_k可以幫助我們查找最后一個維度的 k 個最大條目的值和索引.

格式:

tf.math.top_k(
    input, k=1, sorted=True, name=None
)

參數(shù):

  • input: 傳入張量
  • k=1: 前 k 位
  • sorted: 是否排序
  • name: 數(shù)據(jù)名稱

例子:

# 創(chuàng)建張量0~9, 并打亂順序, 形狀為 3*3
a = tf.reshape(tf.random.shuffle(tf.range(9)), [3, 3])
print(a)

# 取top2
b = tf.math.top_k(a, 2)
print(b)

輸出結(jié)果:

tf.Tensor(
[[2 1 4]
[5 7 0]
[8 6 3]], shape=(3, 3), dtype=int32)
TopKV2(values=<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[4, 2],
[7, 5],
[8, 6]])>, indices=<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[2, 0],
[1, 0],
[0, 1]])>)

填充與復(fù)制

tf.pad

tf.pad可以幫我們對一個 tensor 四周進行填充.

在這里插入圖片描述

格式:

tf.pad(
    tensor, paddings, mode='CONSTANT', constant_values=0, name=None
)

參數(shù):

  • tensor: 傳入的張量
  • paddings: 要擴展的維度
  • mode: 模式, 默認為 “CONSTANT”
  • constant_value: 在 “CONSTANT” 模式下, 要使用的標量填充值 (必須與張量類型相同)
  • name: 數(shù)據(jù)名稱

例子:

# pad
a = tf.reshape(tf.range(9), [3, 3])
print(a)

# 上下左右填充一圈0
b = tf.pad(a, [[1, 1], [1, 1]])
print(b)

輸出結(jié)果:

tf.Tensor(
[[0 1 2]
[3 4 5]
[6 7 8]], shape=(3, 3), dtype=int32)
tf.Tensor(
[[0 0 0 0 0]
[0 0 1 2 0]
[0 3 4 5 0]
[0 6 7 8 0]
[0 0 0 0 0]], shape=(5, 5), dtype=int32)

tf.tile

tf.tile可以幫助我們實現(xiàn) tensor 的復(fù)制.

格式:

tf.tile(
    input, multiples, name=None
)

參數(shù):

  • input: 傳入的張量
  • multiples: 復(fù)制的次數(shù)
  • name: 數(shù)據(jù)名稱

例子:

# tile
a = tf.reshape(tf.range(9), [3, 3])
print(a)

b = tf.tile(a, [2, 2])
print(b)

輸出結(jié)果:

tf.Tensor(
[[0 1 2]
[3 4 5]
[6 7 8]], shape=(3, 3), dtype=int32)
tf.Tensor(
[[0 1 2 0 1 2]
[3 4 5 3 4 5]
[6 7 8 6 7 8]
[0 1 2 0 1 2]
[3 4 5 3 4 5]
[6 7 8 6 7 8]], shape=(6, 6), dtype=int32)

查找與替換

在這里插入圖片描述

tf.where (第一種)

返回元素為 True 的位置.

格式:

tf.where(
    condition, name=None
)

參數(shù):

  • condition: 判斷條件
  • name: 數(shù)據(jù)名稱

例子:

# 第一種用法(單參數(shù))
mask = tf.constant([[True, True, True], [False, True, True], [True, False, False]])
print(mask)

indices = tf.where(mask)
print(indices)

輸出結(jié)果:

tf.Tensor(
[[ True True True]
[False True True]
[ True False False]], shape=(3, 3), dtype=bool)
tf.Tensor(
[[0 0]
[0 1]
[0 2]
[1 1]
[1 2]
[2 0]], shape=(6, 2), dtype=int64)

tf.where (第二種)

類似三元運算符的用法.

格式:

tf.where(
    condition, x=None, y=None, name=None
)

參數(shù):

  • condition: 判斷條件
  • x: 如果條件為 True 賦值
  • y: 如果條件為 False 賦值
  • name: 數(shù)據(jù)名稱

例子:

# 第二種用法(三個參數(shù))
zeros = tf.zeros([3, 3])
print(zeros)

ones = tf.ones([3, 3])
print(ones)

mask = tf.constant([[True, True, True], [False, True, True], [True, False, False]])
print(mask)

result = tf.where(mask, zeros, ones)
print(result)

輸出結(jié)果:

tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[ True True True]
[False True True]
[ True False False]], shape=(3, 3), dtype=bool)
tf.Tensor(
[[0. 0. 0.]
[1. 0. 0.]
[0. 1. 1.]], shape=(3, 3), dtype=float32)

tf.scatter_nd

使用索引更新張量.

在這里插入圖片描述

格式:

tf.scatter_nd(
    indices, updates, shape, name=None
)

參數(shù):

  • indices: 索引
  • updates: 更新的值
  • shape: 形狀
  • name: 數(shù)據(jù)名稱

例子:

# scatter_nd
indices = tf.constant([[4], [3], [1], [7]])
print(indices)

updates = tf.constant([9, 10, 11, 12])
print(updates)

shape = tf.constant([8])
print(shape)

result = tf.scatter_nd(indices, updates, shape)
print(result)

輸出結(jié)果:

tf.Tensor(
[[4]
[3]
[1]
[7]], shape=(4, 1), dtype=int32)
tf.Tensor([ 9 10 11 12], shape=(4,), dtype=int32)
tf.Tensor([8], shape=(1,), dtype=int32)
tf.Tensor([ 0 11 0 10 9 0 0 12], shape=(8,), dtype=int32)

到此這篇關(guān)于TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換的文章就介紹到這了,更多相關(guān)TensorFlow2基本操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python常用編譯器原理及特點解析

    Python常用編譯器原理及特點解析

    這篇文章主要介紹了Python常用編譯器原理及特點解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • 深入理解Python中的Contextlib庫

    深入理解Python中的Contextlib庫

    Python提供了一些內(nèi)建的庫以支持各種常見的編程任務(wù),Contextlib庫是其中之一,它提供了一些用于支持上下文管理協(xié)議(即with語句)的函數(shù),這篇文章將詳細介紹如何使用Contextlib庫中的功能,需要的朋友可以參考下
    2023-06-06
  • python如何生成網(wǎng)頁驗證碼

    python如何生成網(wǎng)頁驗證碼

    這篇文章主要為大家詳細介紹了python如何生成網(wǎng)頁驗證碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • python裝飾器相當于函數(shù)的調(diào)用方式

    python裝飾器相當于函數(shù)的調(diào)用方式

    今天小編就為大家分享一篇python裝飾器相當于函數(shù)的調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python虛擬環(huán)境與依賴管理使用方法全指南

    Python虛擬環(huán)境與依賴管理使用方法全指南

    這篇文章主要介紹了如何使用虛擬環(huán)境和pip來管理Python項目的依賴和包版本,虛擬環(huán)境可以幫助隔離不同項目的依賴,避免版本沖突,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-01-01
  • 詳解Python中的數(shù)據(jù)清洗工具flashtext

    詳解Python中的數(shù)據(jù)清洗工具flashtext

    FlashText是GitHub上的一個開源Python庫,正如之前所提到的,它在提取關(guān)鍵字和替換關(guān)鍵字任務(wù)上有著極高的性能。本文將詳解一下flashtext的使用,需要的可以參考一下
    2022-06-06
  • python中flatten()參數(shù)示例詳解

    python中flatten()參數(shù)示例詳解

    flatten是numpy.ndarray.flatten的一個函數(shù),即返回一個一維數(shù)組,這篇文章主要給大家介紹了關(guān)于python中flatten()參數(shù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-03-03
  • python如何利用joblib保存訓練模型

    python如何利用joblib保存訓練模型

    這篇文章主要介紹了python如何利用joblib保存訓練模型問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Python中元組的概念及應(yīng)用小結(jié)

    Python中元組的概念及應(yīng)用小結(jié)

    Python中的元組和列表很相似,元組也是Python語言提供的內(nèi)置數(shù)據(jù)結(jié)構(gòu)之一,可以在代碼中直接使用,這篇文章主要介紹了Python中元組的概念以及應(yīng)用,需要的朋友可以參考下
    2023-01-01
  • Linux下安裝python3.6和第三方庫的教程詳解

    Linux下安裝python3.6和第三方庫的教程詳解

    這篇文章主要介紹了Linux下安裝python3.6和第三方庫的教程詳解,需要的朋友可以參考下
    2018-11-11

最新評論

彭山县| 民丰县| 精河县| 陇西县| 大庆市| 双江| 乾安县| 谷城县| 即墨市| 普宁市| 万年县| 阳春市| 湖州市| 乌兰察布市| 炎陵县| 尤溪县| 兴隆县| 深圳市| 兴安盟| 息烽县| 阜阳市| 潼关县| 左贡县| 沧源| 来宾市| 南陵县| 新晃| 九江县| 洛隆县| 商都县| 应城市| 伊通| 长岛县| 祁连县| 昌图县| 文昌市| 沙田区| 同德县| 邮箱| 修武县| 湘潭县|