好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > TensorFlow2基本操作之 張量排序 填充與復制 查找與替換

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

熱門標簽:電話機器人適用業務 徐州天音防封電銷卡 不錯的400電話辦理 獲客智能電銷機器人 哈爾濱外呼系統代理商 佛山防封外呼系統收費 湛江電銷防封卡 鄭州智能外呼系統運營商 南昌辦理400電話怎么安裝

張量排序

tf.sort

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

格式:

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

參數:

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

例子:

# 創建張量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)

輸出結果:

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
)

參數:

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

例子:

# 創建張量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)

輸出結果:

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
)

參數:

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

例子:

# 創建張量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)

輸出結果:

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]])>)

填充與復制

tf.pad

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

格式:

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

參數:

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

例子:

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

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

輸出結果:

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可以幫助我們實現 tensor 的復制.

格式:

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

參數:

  • input: 傳入的張量
  • multiples: 復制的次數
  • name: 數據名稱

例子:

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

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

輸出結果:

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
)

參數:

  • condition: 判斷條件
  • name: 數據名稱

例子:

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

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

輸出結果:

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
)

參數:

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

例子:

# 第二種用法(三個參數)
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)

輸出結果:

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
)

參數:

  • indices: 索引
  • updates: 更新的值
  • shape: 形狀
  • name: 數據名稱

例子:

# 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)

輸出結果:

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)

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

您可能感興趣的文章:
  • TensorFlow2基本操作之合并分割與統計
  • 一小時學會TensorFlow2之基本操作1實例代碼
  • 一小時學會TensorFlow2之基本操作2實例代碼

標簽:紹興 蘭州 吉安 安康 廣西 呂梁 蕪湖 懷化

巨人網絡通訊聲明:本文標題《TensorFlow2基本操作之 張量排序 填充與復制 查找與替換》,本文關鍵詞  TensorFlow2,基本操作,之,張量,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《TensorFlow2基本操作之 張量排序 填充與復制 查找與替換》相關的同類信息!
  • 本頁收集關于TensorFlow2基本操作之 張量排序 填充與復制 查找與替換的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 女人自慰一级看片免费一区二区| 欧洲美女与动交ZOZ0Z| 欧美护士猛交ⅩXXX乱大交| 色噜噜狠狠色综合成人网| 男人cao女人视频在线观看| 男人同性**毛片| 中文字幕在线精品| 中文字幕乱偷无码亚洲A片| 纯肉巨黄H爆口男男分卷阅读| 欧美野外性xxxxfeexxxx| 女人一旦尝试过3p后会怎么样| 粗莽糙汉攻双性受| 久久91精品综合国产首页| 嗯啊不要嗯啊| 91在线精品无码秘?入口软件| 勾搭足浴女技师国产在线| 好想被cao啊随便cao| 国产成人麻豆精品video| 黑人超长巨大XXXXXXX白人| 国产精品18久久久久久不卡 | 女兵性史bd| 永久在线观看免费视频| 亚洲se网| 久久99久久99精品免视看婷婷 | 大白腿美女屁股啪啪网站| av亚洲产国偷v产偷v自拍小说| 国产一级A片毛毛天码美女视频| 欧美成人怡红院在线观看| 雷电将军被爆羞羞漫画小说| 啊插到子宫里了h| 美女又爽?又黄?免费跳舞软件 | 公交车强摁做开腿呻吟| 亚洲性电影| 北岛玲在厨房被猛烈进入| 女学生被?c??扒衣服| 美女被人插| 宝贝直播在线观看高清视频免费| 工地诱奷H系列高辣h彤奴婷奴| 熟女???码免费播放www| 日本理伦在线| 韩国19女主播内部vip348|