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

一小時學(xué)會TensorFlow2之基本操作2實例代碼

 更新時間:2021年09月03日 15:36:35   作者:我是小白呀  
這篇文章主要介紹了TensorFlow2的基本操作和實例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

索引操作

在這里插入圖片描述

簡單索引

索引 (index) 可以幫助我們快速的找到張量中的特定信息.

例子:

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

print(a[0])
print(a[0][0])

輸出結(jié)果:

tf.Tensor(
[[[ 0 1 2]
[ 3 4 5]]

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

Numpy 式索引

我們也可以按照 numpy 的寫法來操作索引.

例子:

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

print(a[0])
print(a[0, 0])

輸出結(jié)果:

tf.Tensor(
[[[ 0 1 2]
[ 3 4 5]]

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

使用 : 進行索引

例子:

c = tf.ones([4, 14, 14, 4])
print(c[0, :, :, :].shape)
print(c[0, 1, :, :].shape)

輸出結(jié)果:

(14, 14, 4)
(14, 4)

tf.gather

我們假設(shè)一個有 3 個餐館, 每個餐館有 8 種菜系, 128 道菜data: [resturants, cuisines, dishes].

在這里插入圖片描述

例子:

data = tf.zeros([3, 8, 128])

g1 = tf.gather(data, axis=0, indices=[0, 2])
print(g1.shape)

g2 = tf.gather(data, axis=1, indices=[0, 1, 2, 3])
print(g2.shape)

輸出結(jié)果:

(2, 8, 128)
(3, 4, 128)

tf.gather_nd

例子:

g1 = tf.gather_nd(data, [0])
print(g1.shape)

g2 = tf.gather_nd(data, [0, 1])
print(g2.shape)

g3 = tf.gather_nd(data, [0, 1, 2])
print(g3.shape)

輸出結(jié)果:

(8, 128)
(128,)
()

tf.boolean_mask

格式:

tf.boolean_mask(
    tensor, mask, axis=None, name='boolean_mask'
)

例子:

data = tf.zeros([3, 8, 128])

b1 = tf.boolean_mask(data, mask=[True, True, False])
print(b1.shape)

b2 = tf.boolean_mask(data, mask=[True, False, True, False, True, False, True, False], axis=1)
print(b2.shape)

輸出結(jié)果:

(2, 8, 128)
(3, 4, 128)

切片操作

借助切片技術(shù), 我們可以靈活的處理張量對象.

在這里插入圖片描述

簡單切片

格式:

tensor[start : end]

其中 start 為開始索引, end 為結(jié)束索引 (不包括)

例子:

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

step 切片

格式:

tensor[start : end: step]

例子:

d = tf.range(6)
print(d[::-1])  # 實現(xiàn)倒序
print(d[::2])  # 步長為2

輸出結(jié)果:

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

維度變換

在這里插入圖片描述

tf.reshape

tf.reshape 可以幫助我們進行維度轉(zhuǎn)換.

格式:

tf.reshape(
    tensor, shape, name=None
)

參數(shù):

  • tensor: 傳入的張量
  • shape: 張量的形狀
  • name: 數(shù)據(jù)名稱

例子:

a = tf.random.normal([3, 8, 128])
print(a.shape)

b = tf.reshape(a, [3, 1024])
print(b.shape)

c = tf.reshape(a, [3, -1])
print(c.shape)

輸出結(jié)果:

(3, 8, 128)
(3, 1024)
(3, 1024)

tf.transpose

格式:

tf.transpose(
    a, perm=None, conjugate=False, name='transpose'
)

例子:

a = tf.random.normal([4, 3, 2, 1])
print(a.shape)

b = tf.transpose(a)
print(b.shape)

c = tf.transpose(a, perm=[0, 1, 3, 2])
print(c.shape)

輸出結(jié)果:

(4, 3, 2, 1)
(1, 2, 3, 4)
(4, 3, 1, 2)

tf.expand_dims

格式:

tf.expand_dims(
    input, axis, name=None
)

參數(shù):

  • input: 輸入
  • axis: 操作的維度
  • name: 數(shù)據(jù)名稱

例子:

a = tf.random.normal([4, 3, 2, 1])
print(a.shape)

b = tf.expand_dims(a, axis=0)
print(b.shape)

c = tf.expand_dims(a, axis=1)
print(c.shape)

d = tf.expand_dims(a, axis=-1)
print(d.shape)

輸出結(jié)果:

(4, 3, 2, 1)
(1, 4, 3, 2, 1)
(4, 1, 3, 2, 1)
(4, 3, 2, 1, 1)

tf.squeeze

tf.squeeze 可以幫助我們刪去所有維度為1 的維度.

在這里插入圖片描述

格式:

tf.squeeze(
    input, axis=None, name=None
)

參數(shù):

  • input: 輸入
  • axis: 操作的維度
  • name: 數(shù)據(jù)名稱

例子:

a = tf.zeros([2, 1, 1, 3, 5])

s1 = tf.squeeze(a)
print(s1.shape)

s2 = tf.squeeze(a, axis=1)
print(s2.shape)

s3 = tf.squeeze(a, axis=2)
print(s3.shape)

輸出結(jié)果:

(2, 3, 5)
(2, 1, 3, 5)
(2, 1, 3, 5)

Boardcasting

廣播機制 (Boardcasting) 是一種張量復(fù)制的手段. Boardcasting 可以幫助我們擴張張量的形狀但無需實際復(fù)制數(shù)據(jù).

在這里插入圖片描述

廣播機制允許我們在隱式情況下進行填充, 從而使得我們的代碼更加簡潔, 更有效率地使用內(nèi)存.

tf.boardcast_to

boardcast_to:

tf.broadcast_to(
    input, shape, name=None
)

參數(shù):

  • input: 輸入
  • shape: 數(shù)據(jù)形狀
  • name: 數(shù)據(jù)名稱

例子:

a = tf.broadcast_to(tf.random.normal([4, 1, 1, 1]), [4, 32, 32, 3])
print(a.shape)

b = tf.broadcast_to(tf.zeros([128, 1, 1, 1]), [128, 32, 32, 3])
print(b.shape)

輸出結(jié)果:

(4, 32, 32, 3)
(128, 32, 32, 3)

tf.tile

格式:

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

參數(shù):

  • input: 輸入
  • multiples: 同一緯度上復(fù)制的次數(shù)
  • name: 數(shù)據(jù)名稱

例子:

a = tf.zeros([4, 1, 1, 1])
print(a.shape)

b = tf.tile(a, [1, 32, 32, 3])
print(b.shape)

輸出結(jié)果:

(4, 1, 1, 1)
(4, 32, 32, 3)

注: boardcast_to 和 tile 的區(qū)別在于 boardcast_to 可以在不復(fù)制內(nèi)存的情況下自動擴張 tensor.

數(shù)學(xué)運算

在這里插入圖片描述

加減乘除

例子:

# 定義張量
t1 = tf.ones([3, 3])
t2 = tf.fill([3, 3], 3.0)

# 加
add = t1 + t2
print(add)

# 減
minus = t1 - t2
print(minus)

# 乘
multiply = t1 * t2
print(multiply)

# 除
divide = t1 / t2
print(divide)

輸出結(jié)果:

tf.Tensor(
[[4. 4. 4.]
[4. 4. 4.]
[4. 4. 4.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[-2. -2. -2.]
[-2. -2. -2.]
[-2. -2. -2.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[3. 3. 3.]
[3. 3. 3.]
[3. 3. 3.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[0.33333334 0.33333334 0.33333334]
[0.33333334 0.33333334 0.33333334]
[0.33333334 0.33333334 0.33333334]], shape=(3, 3), dtype=float32)

log & exp

例子:

# log
a = tf.fill([2], 100.0)
print(a)

b = tf.math.log(a)  # 以e為底
print(b)

# exp
c = tf.ones([2])
print(c)

d = tf.exp(c)
print(d)

輸出結(jié)果:

tf.Tensor([100. 100.], shape=(2,), dtype=float32)
tf.Tensor([4.6051702 4.6051702], shape=(2,), dtype=float32)
tf.Tensor([1. 1.], shape=(2,), dtype=float32)
tf.Tensor([2.7182817 2.7182817], shape=(2,), dtype=float32)

pow & sqrt

例子:

# 定義張量
a = tf.fill([2], 4.0)
print(a)

# pow
b = tf.pow(a, 2)
print(b)

# sqrt
c = tf.sqrt(a, 2)
print(c)

輸出結(jié)果:

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

矩陣相乘 @

我們可以使用tf.matmul@來實現(xiàn)矩陣相乘.

在這里插入圖片描述

例子:

# 定義張量
a = tf.fill([2, 2], 2)
b = tf.fill([2, 2], 3)

# matmul
c = tf.matmul(a, b)
print(c)

# @
d = a@b
print(d)

輸出結(jié)果:

tf.Tensor(
[[12 12]
[12 12]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[12 12]
[12 12]], shape=(2, 2), dtype=int32)

到此這篇關(guān)于一小時學(xué)會TensorFlow2之基本操作2實例代碼的文章就介紹到這了,更多相關(guān)TensorFlow2基本操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python 專題三 字符串的基礎(chǔ)知識

    Python 專題三 字符串的基礎(chǔ)知識

    在Python中最重要的數(shù)據(jù)類型包括字符串、列表、元組和字典等。本篇文章主要講述Python的字符串基礎(chǔ)知識。下面跟著小編一起來看下吧
    2017-03-03
  • mac下pip、conda、homebrew修改為清華鏡像源的方法

    mac下pip、conda、homebrew修改為清華鏡像源的方法

    本文主要介紹了mac下pip、conda、homebrew修改為清華鏡像源的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Python數(shù)據(jù)類型之列表和元組的方法實例詳解

    Python數(shù)據(jù)類型之列表和元組的方法實例詳解

    這篇文章主要介紹了Python數(shù)據(jù)類型之列表和元組的相關(guān)知識,列表是一組有序項目的集合 ,可變的數(shù)據(jù)類型可 進行增刪改查,本文通過實例文字相結(jié)合的形式給大家介紹的非常詳細 ,需要的朋友可以參考下
    2019-07-07
  • python tkinter組件使用詳解

    python tkinter組件使用詳解

    這篇文章主要介紹了python tkinter組件使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2019-09-09
  • python中opencv K均值聚類的實現(xiàn)示例

    python中opencv K均值聚類的實現(xiàn)示例

    本文主要介紹了python中opencv K均值聚類的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2022-06-06
  • Flask快速實現(xiàn)分頁效果示例

    Flask快速實現(xiàn)分頁效果示例

    本文主要介紹了Flask快速實現(xiàn)分頁效果示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2022-08-08
  • keras之權(quán)重初始化方式

    keras之權(quán)重初始化方式

    這篇文章主要介紹了keras之權(quán)重初始化方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • pycharm創(chuàng)建django項目出現(xiàn)路徑錯誤問題

    pycharm創(chuàng)建django項目出現(xiàn)路徑錯誤問題

    在PyCharm中創(chuàng)建Django項目時,若使用之前項目的環(huán)境編譯器,且已修改其根目錄,則新建項目路徑可能出錯。解決辦法是在設(shè)置中選擇Project,通過齒輪圖標進入Show?All,選擇編譯器路徑,點擊筆形圖修改Development?configuration的Root?path為/,以確保新項目能正確創(chuàng)建
    2024-09-09
  • 用Python實現(xiàn)一個簡單的能夠上傳下載的HTTP服務(wù)器

    用Python實現(xiàn)一個簡單的能夠上傳下載的HTTP服務(wù)器

    這篇文章主要介紹了用Python實現(xiàn)一個簡單的能夠上傳下載的HTTP服務(wù)器,是Python網(wǎng)絡(luò)編程學(xué)習當中的基礎(chǔ),本文示例基于Windows操作系統(tǒng)實現(xiàn),需要的朋友可以參考下
    2015-05-05
  • 利用Tkinter(python3.6)實現(xiàn)一個簡單計算器

    利用Tkinter(python3.6)實現(xiàn)一個簡單計算器

    這篇文章主要給大家介紹了關(guān)于利用Tkinter(python3.6)實現(xiàn)一個簡單計算器的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧。
    2017-12-12

最新評論

南投县| 临潭县| 忻城县| 梅河口市| 西昌市| 陆丰市| 长丰县| 吉安县| 兴和县| 宣化县| 微山县| 香河县| 竹溪县| 英吉沙县| 墨玉县| 辽源市| 新乡市| 和林格尔县| 从江县| 房产| 海伦市| 武夷山市| 南郑县| 洛南县| 南涧| 申扎县| 高碑店市| 通化市| 横山县| 梁平县| 平湖市| 保靖县| 兴国县| 酒泉市| 济源市| 华安县| 濉溪县| 新绛县| 河东区| 丰都县| 庄浪县|