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

TensorFlow2基本操作之合并分割與統(tǒng)計(jì)

 更新時(shí)間:2021年09月08日 10:34:58   作者:我是小白呀  
這篇文章主要介紹了TensorFlow2基本操作之合并分割與統(tǒng)計(jì),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

合并與分割

在這里插入圖片描述

tf.concat

tf.concat可以幫助我們實(shí)現(xiàn)拼接操作.

格式:

tf.concat(
    values, axis, name='concat'
)

參數(shù):

  • values: 一個(gè) tensor 或 tensor list
  • axis: 操作的維度
  • name: 數(shù)據(jù)名稱, 默認(rèn)為 “concat”

例子:

part_1 = tf.zeros([5, 3])
print(part_1)

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

# 豎向拼接
result_1 = tf.concat([part_1, part_2], axis=0)
print(result_1)

# 橫向拼接
result_2 = tf.concat([part_1, part_2], axis=1)
print(result_2)

輸出結(jié)果:

tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(10, 3), dtype=float32)
tf.Tensor(
[[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]], shape=(5, 6), dtype=float32)

tf.stack

rf.stack可以創(chuàng)建一個(gè)新的維度來合并兩個(gè)張量.

在這里插入圖片描述

格式:

tf.stack(
    values, axis=0, name='stack'
)

參數(shù):

  • values: 一個(gè) tensor list
  • axis: 操作的維度
  • name: 數(shù)據(jù)名稱, 默認(rèn)為 “stack”

例子:

part_1 = tf.zeros([5, 3])
print(part_1)

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

# 頭拼接
result_1 = tf.stack([part_1, part_2], axis=0)
print(result_1)

# 尾拼接
result_2 = tf.stack([part_1, part_2], axis=2)
print(result_2)

輸出結(jié)果:

tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
tf.Tensor(
[[[0. 1.]
[0. 1.]
[0. 1.]]

[[0. 1.]
[0. 1.]
[0. 1.]]

[[0. 1.]
[0. 1.]
[0. 1.]]

[[0. 1.]
[0. 1.]
[0. 1.]]

[[0. 1.]
[0. 1.]
[0. 1.]]], shape=(5, 3, 2), dtype=float32)

tf.unstack

tf.unstack是一個(gè)矩陣分解函數(shù).

格式:

# unstack
tf.unstack(
value, num=None, axis=0, name='unstack'
)

參數(shù):

  • values: 一個(gè) tensor, 維度大于 0
  • num: 軸的長(zhǎng)度
  • axis: 操作的維度
  • name: 數(shù)據(jù)名稱, 默認(rèn)為 “unstack”

例子:

a = tf.stack([tf.zeros([5, 3]), tf.ones([5, 3])], axis=0)
print(a)

b = tf.unstack(a, axis=0)
print(b)

輸出結(jié)果:

tf.Tensor(
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
[<tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>, <tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>]

tf.split

tf.split()可以把一個(gè)張量劃分為幾個(gè)子張量.

在這里插入圖片描述

格式:

tf.split(
    value, num_or_size_splits, axis=0, num=None, name='split'
)

參數(shù):

  • value: 待切分的張量
  • num_or_size_splits: 切成幾份
  • axis: 操作的維度
  • num: num_or_size_splits 不能實(shí)現(xiàn)的情況下使用
  • name: 數(shù)據(jù)名稱, 默認(rèn)為 “split”

例子:

# split
a = tf.stack([tf.zeros([5, 3]), tf.ones([5, 3])], axis=0)
print(a)

b = tf.split(a, 2)
print(b)

輸出結(jié)果:

tf.Tensor(
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
[<tf.Tensor: shape=(1, 5, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]], dtype=float32)>, <tf.Tensor: shape=(1, 5, 3), dtype=float32, numpy=
array([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]], dtype=float32)>]

數(shù)據(jù)統(tǒng)計(jì)

在這里插入圖片描述

tf.norm

tf.norm可以幫助我們計(jì)算向量, 矩陣, 張量的范數(shù).

格式:

tf.norm(
    tensor, ord='euclidean', axis=None, keepdims=None, name=None
)

參數(shù):

  • tensor: 輸入的張量
  • ord: 范數(shù)的順序
  • axis: 操作的維度
  • keep_dims: 如果為 True, 則 axis 中指定的軸將保持為大小 1
  • name: 數(shù)據(jù)名稱

例子:

a = tf.fill([2, 2], 2.0)
print(a)

# sqrt(2^2 * 4) = sqrt(16) = 4
b = tf.norm(a)
print(b)

# [2 + 2, 2 + 2] = [4, 4]
c = tf.norm(a, ord=1, axis= 0)
print(c)

# [sqrt(2^2 + 2^2), sqrt(2^2 + 2^2)] = [sqrt(8), sqrt(8)]
d = tf.norm(a, ord=2, axis= 0)
print(d)

輸出結(jié)果:

tf.Tensor(
[[2. 2.]
[2. 2.]], shape=(2, 2), dtype=float32)
tf.Tensor(4.0, shape=(), dtype=float32)
tf.Tensor([4. 4.], shape=(2,), dtype=float32)
tf.Tensor([2.828427 2.828427], shape=(2,), dtype=float32)

reduce_min/max/mean

計(jì)算一個(gè)張量各個(gè)維度上元素的最小值 / 最大值 / 平均值.

格式:

tf.math.reduce_min / reduce_max / reduce_mean(
    input_tensor, axis=None, keepdims=False, name=None
)

參數(shù):

  • input_tensor: 傳入的張量
  • axis: 維度, 默認(rèn)計(jì)算所有維度
  • keepdims: 如果為真保留維度, 默認(rèn)為 False
  • name: 數(shù)據(jù)名稱

例子:

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

min = tf.reduce_min(a)
print(min)

max = tf.reduce_max(a)
print(max)

輸出結(jié)果:

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

argmax / argmin

tf.argmax/tf.argmin可以幫我們找到最大 / 最小值所在的索引 (index).

格式:

tf.math.argmax(
    input, axis=None, output_type=tf.dtypes.int64, name=None
)

參數(shù):

  • input: 輸入
  • axis: 操作的維度
  • output_type: 輸出數(shù)據(jù)類型, 默認(rèn)為 int64
  • name: 數(shù)據(jù)名稱

例子:

# argmax / argmin
a = tf.reshape(tf.range(9), [3, 3])
print(a)

max = tf.argmax(a)
print(max)

min = tf.argmin(a)
print(min)

輸出結(jié)果:

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

tf.equal

tf.equal可以幫助我們判斷兩個(gè)張量是否相等. 返回 True / False.

在這里插入圖片描述

格式:

tf.math.equal(
    x, y, name=None
)

例子:

a = tf.zeros(5, dtype=tf.float32)
print(a)

b = tf.range(5, dtype=tf.float32)
print(b)

print(tf.equal(a, b))

輸出結(jié)果:

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

tf.unique

tf.unique可以幫我們找出張量中不重復(fù)的值

格式:

tf.unique(
    x, out_idx=tf.dtypes.int32, name=None
)

參數(shù):

  • input: 輸入
  • output_type: 輸出數(shù)據(jù)類型, 默認(rèn)為 int32
  • name: 數(shù)據(jù)名稱

例子:

a = tf.range(5)
print(tf.unique(a))

b = tf.constant([4, 2, 2, 4, 3])
print(tf.unique(b))

輸出結(jié)果:

Unique(y=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>)
Unique(y=<tf.Tensor: shape=(3,), dtype=int32, numpy=array([4, 2, 3])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2])>)

到此這篇關(guān)于一小時(shí)學(xué)會(huì)TensorFlow2基本操作之合并分割與統(tǒng)計(jì)的文章就介紹到這了,更多相關(guān)TensorFlow2合并分割與統(tǒng)計(jì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python人工智能tensorflow函數(shù)tf.assign使用方法

    python人工智能tensorflow函數(shù)tf.assign使用方法

    這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.assign使用方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • pyqt qlistwidget改變item顏色的操作

    pyqt qlistwidget改變item顏色的操作

    這篇文章主要介紹了pyqt qlistwidget改變item顏色的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)根據(jù)字段將記錄分組操作示例

    Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)根據(jù)字段將記錄分組操作示例

    這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)根據(jù)字段將記錄分組操作,結(jié)合實(shí)例形式分析了itertools.groupby()函數(shù)針對(duì)字典進(jìn)行分組操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-03-03
  • matplotlib畫圖之修改坐標(biāo)軸刻度問題

    matplotlib畫圖之修改坐標(biāo)軸刻度問題

    這篇文章主要介紹了matplotlib畫圖之修改坐標(biāo)軸刻度問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python在終端通過pip安裝好包以后在Pycharm中依然無法使用的問題(三種解決方案)

    Python在終端通過pip安裝好包以后在Pycharm中依然無法使用的問題(三種解決方案)

    這篇文章主要介紹了Python在終端通過pip安裝好包以后在Pycharm中依然無法使用的問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python屬于解釋型語言么

    python屬于解釋型語言么

    在本篇文章里小編給大家整理了關(guān)于python是否屬于解釋型語言的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • caffe binaryproto 與 npy相互轉(zhuǎn)換的實(shí)例講解

    caffe binaryproto 與 npy相互轉(zhuǎn)換的實(shí)例講解

    今天小編就為大家分享一篇caffe binaryproto 與 npy相互轉(zhuǎn)換的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python配置虛擬環(huán)境圖文步驟

    Python配置虛擬環(huán)境圖文步驟

    在本文中我們給大家詳細(xì)整理了關(guān)于Python配置虛擬環(huán)境的相關(guān)步驟以及圖文說明,需要的朋友們學(xué)習(xí)下。
    2019-05-05
  • Python根據(jù)文件后綴實(shí)現(xiàn)文件夾整理

    Python根據(jù)文件后綴實(shí)現(xiàn)文件夾整理

    這篇文章主要為大家詳細(xì)介紹了Python如何根據(jù)文件后綴實(shí)現(xiàn)文件夾整理,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的可以參考下
    2024-02-02
  • Python中zip()函數(shù)的使用方法詳解

    Python中zip()函數(shù)的使用方法詳解

    在Python中,zip()函數(shù)是一個(gè)非常實(shí)用且強(qiáng)大的內(nèi)置函數(shù),它主要用于將多個(gè)迭代器(如列表、元組、字符串等)中的元素“打包”成一個(gè)個(gè)元組,并返回一個(gè)迭代器,下面,我將詳細(xì)探討zip()函數(shù)的使用方法,需要的朋友可以參考下
    2024-09-09

最新評(píng)論

神农架林区| 河南省| 洞口县| 米泉市| 泉州市| 凤台县| 北宁市| 航空| 酒泉市| 聊城市| 西昌市| 蚌埠市| 温泉县| 资阳市| 奉贤区| 惠州市| 德州市| 梁山县| 合肥市| 蒲江县| 渝北区| 阿克陶县| 上虞市| 于田县| 湘西| 施秉县| 中阳县| 丽水市| 扬州市| 兴海县| 施秉县| 灵璧县| 左云县| 荃湾区| 平安县| 浪卡子县| 吉隆县| 慈利县| 安庆市| 贵州省| 五莲县|