人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

一小時(shí)學(xué)會(huì)TensorFlow2之基本操作1實(shí)例代碼

發(fā)布日期:2022-01-29 08:26 | 文章來(lái)源:腳本之家

概述

TensorFlow2 的基本操作和 Numpy 的操作很像. 今天帶大家來(lái)看一看 TensorFlow 的基本數(shù)據(jù)操作.

創(chuàng)建數(shù)據(jù)

詳細(xì)講解一下 TensorFlow 創(chuàng)建數(shù)據(jù)的集中方法.

創(chuàng)建常量

tf.constant() 格式為:

tf.constant(value,dtype,shape,name)

參數(shù):

  • value: 常量值
  • dtype: 數(shù)據(jù)類型
  • shape: 表示生成常量數(shù)的維度
  • name: 數(shù)據(jù)名稱

例子:

# 創(chuàng)建常量1
c1 = tf.constant(1)
print(c1)
# 創(chuàng)建常量, 類型為bool
c2 = tf.constant([True, False])
print(c2)
# 創(chuàng)建常量1, 類型為float32, 大小為3*3
c3 = tf.constant(0.1, shape=[2, 2])
print(c3)
# 創(chuàng)建常量, 類型為string字符串
c4 = tf.constant("Hello World!")
print(c4)

輸出結(jié)果:

tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor([ True False], shape=(2,), dtype=bool)
tf.Tensor(
[[0.1 0.1]
[0.1 0.1]], shape=(2, 2), dtype=float32)
tf.Tensor(b'Hello World!', shape=(), dtype=string)

創(chuàng)建數(shù)據(jù)序列

格式:

range(start, limit, delta=1, dtype=None, name='range')

參數(shù):

  • start: 開始位置
  • limit: 序列的上限
  • delta: 相當(dāng)于 Numpy 的 step, 步長(zhǎng)
  • detype: 數(shù)據(jù)類型
  • name: 數(shù)據(jù)名稱, 默認(rèn)為 “range”

例子:

# 創(chuàng)建數(shù)字序列
r1 = tf.range(4)
print(r1)

輸出結(jié)果:

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

創(chuàng)建圖變量

格式:

tf.Variable.init(initial_value, trainable=True, collections=None, validate_shape=True, name=None)

參數(shù):

參數(shù)名稱 參數(shù)類型 參數(shù)含義
initial_value 所有可以轉(zhuǎn)換為 Tensor 的類型 變量的初始值
trainable bool 如果為 True, 會(huì)把它加入到 GraphKeys.TRAINABLE_VARIABLES, 才能對(duì)它使用 Optimizer
collections list 指定該圖變量的類型, 默認(rèn)為 [GraphKeys.GLOBAL_VARIABLES]
validate_shape bool 如果為 False, 則不進(jìn)行類型和維度檢查
name string 數(shù)據(jù)名稱

例子:

# 創(chuàng)建圖變量
v1 = tf.Variable(tf.range(6))
print(v1)
print(isinstance(v1, tf.Tensor))  # False
print(isinstance(v1, tf.Variable))  # True
print(tf.is_tensor(v1))  # True

輸出結(jié)果:

False
True
True

tf.zeros

tf.zeros 可以幫助我們創(chuàng)建一個(gè)所有參數(shù)為 0 的 tensor 對(duì)象. 類似于 np.zeros.

格式:

tf.zeros(shape, dtype=tf.dtypes.float32, name=None)

參數(shù):

  • shape: 數(shù)組的形狀
  • dype: 數(shù)據(jù)類型, 默認(rèn)為float32
  • name: 數(shù)據(jù)名稱

例子:

# 創(chuàng)建參數(shù)為0的tensor
z1 = tf.zeros([1])
print(z1)
z2 = tf.zeros([3, 3])
print(z2)

輸出結(jié)果:

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

tf.ones

tf.ones 用法和 tf.zeros 一樣, 可以幫助我們創(chuàng)建一個(gè)所有參數(shù)為 1 的 tensor 對(duì)象.

tf.ones(shape, dtype=tf.dtypes.float32, name=None)

參數(shù):

  • shape: 數(shù)組的形狀
  • dype: 數(shù)據(jù)類型, 默認(rèn)為 float32
  • name: 數(shù)據(jù)名稱

例子:

# 創(chuàng)建參數(shù)為1的tensor
o1 = tf.ones([1])
print(o1)
o2 = tf.ones([3, 3])
print(o2)

輸出結(jié)果:

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

tf.zeros_like

tf.zeros_like 可以幫我們創(chuàng)建一個(gè)與給定 tensor 類型大小一致的 tensor. 類似 np.zeros_like.

格式:

tf.zeros_like(tensor, dype=None, name=None)

參數(shù):

  • tensor: 傳入的 tensor
  • dype: 數(shù)據(jù)類型, 默認(rèn)為 float32
  • name: 數(shù)據(jù)名稱

例子:

# tf.zeros_like
t1 = tf.range(6)
z1 = tf.zeros_like(t1)
print(z1)

輸出結(jié)果:

tf.Tensor([0 0 0 0 0 0], shape=(6,), dtype=int32)

tf.ones_like

格式:

tf.ones_like(tensor, dype=None, name=None)

參數(shù):

  • tensor: 傳入的 tensor
  • dype: 數(shù)據(jù)類型, 默認(rèn)為 float32
  • name: 數(shù)據(jù)名稱

例子:

# tf.ones_like
t1 = tf.range(6)
o1 = tf.ones_like(t1)
print(o1)

輸出結(jié)果:

tf.Tensor([1 1 1 1 1 1], shape=(6,), dtype=int32)

tf.fill

tf.fill 可以幫助我們創(chuàng)建一個(gè)指定形狀和內(nèi)容的 tensor.

格式:

tf.fill(shape, value, name=None)

參數(shù):

  • shape: 數(shù)組的形狀
  • value: 填充的值
  • name: 數(shù)據(jù)名稱

例子:

# tf.fill
f1 = tf.fill([2, 2], 0)
print(f1)
f2 = tf.fill([3, 3], 6)
print(f2)

輸出結(jié)果:

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

tf.gather

tf.gather: 根據(jù)索引從參數(shù)軸收集切片.

格式:

tf.gather(
 params, indices, validate_indices=None, axis=None, batch_dims=0, name=None
)

參數(shù):

  • params: 傳入的張量
  • indices: A Tensor. types 必須是: int32, int64. 里面的每一個(gè)元素大小必須在 [0, params.shape[axis]) 范圍內(nèi)
  • axis: 維度, 默認(rèn)為 0

例子:

input =[ [[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]],
 
[[[7, 7, 7], [8, 8, 8]],
[[9, 9, 9], [10, 10, 10]],
[[11, 11, 11], [12, 12, 12]]],
 
  [[[13, 13, 13], [14, 14, 14]],
[[15, 15, 15], [16, 16, 16]],
[[17, 17, 17], [18, 18, 18]]]
]
output=tf.gather(input, [0,2],axis=0)

輸出結(jié)果:

tf.Tensor(
[[[[ 1 1 1]
[ 2 2 2]]

[[ 3 3 3]
[ 4 4 4]]

[[ 5 5 5]
[ 6 6 6]]]


[[[13 13 13]
[14 14 14]]

[[15 15 15]
[16 16 16]]

[[17 17 17]
[18 18 18]]]], shape=(2, 3, 2, 3), dtype=int32)

tf.random

正態(tài)分布

tf.random.normal 可以幫我們創(chuàng)建隨機(jī)數(shù)服從正態(tài)分布.

格式:

tf.random.normal(
 shape, mean=0.0, stddev=1.0, dtype=tf.dtypes.float32, seed=None, name=None
)

參數(shù):

  • shape: 張量的形狀
  • mean: 正態(tài)分布的均值, 默認(rèn)為 0.0
  • stddev: 正態(tài)分布的標(biāo)準(zhǔn)差, 默認(rèn)為 1.0
  • dtype: 數(shù)據(jù)類型, 默認(rèn)為 float32
  • seed: 隨機(jī)數(shù)種子
  • name: 數(shù)據(jù)名稱

例子:

# tf.normal
n1 = tf.random.normal([2, 2], mean = 1, stddev=1, seed=0)
print(n1)

輸出結(jié)果:

tf.Tensor(
[[0.60084236 3.1044393 ]
[1.1710722 1.5465181 ]], shape=(2, 2), dtype=float32)

均勻分布

tf.random.uniform 可以幫我們創(chuàng)建隨機(jī)數(shù)服從均勻分布.
格式:

tf.random.uniform(
 shape, minval=0, maxval=None, dtype=tf.dtypes.float32, seed=None, name=None
)

參數(shù):

  • shape: 張量的形狀
  • minval: 均勻分布的最小值, 默認(rèn)為 0
  • maxvak: 均勻分布的最大值
  • dtype: 數(shù)據(jù)類型, 默認(rèn)為 float32
  • seed: 隨機(jī)數(shù)種子
  • name: 數(shù)據(jù)名稱

例子:

# tf.uniform
u1 = tf.random.uniform([2, 2], minval=0, maxval=1)
print(u1)

輸出結(jié)果:

tf.Tensor(
[[0.7382153 0.6622821 ]
[0.22840345 0.09706533]], shape=(2, 2), dtype=float32)

打亂順序

tf.random.shuffle 可以幫助我們打亂張量的順序.

格式:

tf.random.shuffle(
 value, seed=None, name=None
)

參數(shù):

  • value: 要被打亂的張量
  • seed: 隨機(jī)數(shù)種子
  • name: 數(shù)據(jù)名稱

例子:

# tf.shuffle
s1 = tf.random.shuffle(tf.range(10))
print(s1)

輸出結(jié)果:

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

獲取數(shù)據(jù)信息

獲取數(shù)據(jù)維度

tf.rank 的用法和 np.ndim 基本一樣.

格式:

rank(input, name=None)  # 類似np.ndim

參數(shù):

  • input: 傳入的張量
  • name: 數(shù)據(jù)名稱

例子:

# 獲取張量維度
t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
print(tf.rank(t))

輸出結(jié)果:

tf.Tensor(3, shape=(), dtype=int32)

數(shù)據(jù)是否為張量

格式:

tf.is_tensor(input)

參數(shù):

  • input: 傳入的張量

例子:

# 判斷是否為張量
a = tf.constant([1, 2, 3])
b = tf.constant([True, False, False])
c = tf.constant("Hello World")
d = np.arange(6)
print(a)
print(tf.is_tensor(a))
print(b)
print(tf.is_tensor(b))
print(c)
print(tf.is_tensor(c))
print(d)
print(tf.is_tensor(d))

輸出結(jié)果:

tf.Tensor([1 2 3], shape=(3,), dtype=int32)
True
tf.Tensor([ True False False], shape=(3,), dtype=bool)
True
tf.Tensor(b'Hello World', shape=(), dtype=string)
True
[0 1 2 3 4 5]
False

數(shù)據(jù)轉(zhuǎn)換

轉(zhuǎn)換成張量

格式:

tf.convert_to_tensor(value, dtype=None, dtype_hint=None, name=None)

參數(shù):

  • value: 需要轉(zhuǎn)換的值
  • dtype: 數(shù)據(jù)類型
  • dtype_hint: 當(dāng) dtype 為 None 時(shí)的備選方案
  • name: 數(shù)據(jù)名稱

例子:

# 轉(zhuǎn)換成張量
array = np.arange(6)
print(array.dtype)
array_tf = tf.convert_to_tensor(array)
print(array_tf)

輸出結(jié)果:

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

轉(zhuǎn)換數(shù)據(jù)類型

格式:

cast(x, dtype, name=None)

參數(shù):

  • x: 輸入的值
  • dtype: 數(shù)據(jù)類型
  • name: 數(shù)據(jù)名稱

例子:

# 裝換數(shù)據(jù)類型
array_tf = tf.constant(np.arange(6))
print(array_tf)
array_tf = tf.cast(array_tf, dtype=tf.float32)
print(array_tf)
tf_bool = tf.cast(tf.constant([False, True]), dtype=tf.int32)
print(tf_bool)

輸出結(jié)果:

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

轉(zhuǎn)換成 numpy

例子:

# tensor轉(zhuǎn)換成numpy
array_tf = tf.ones([2,2])
array_np = array_tf.numpy()
print(array_np)

輸出結(jié)果:

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

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

美國(guó)服務(wù)器租用

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部