TensorFlow2 一小時學(xué)會基本操作 1
https://iamarookie.blog.csdn.net/article/details/117591977
TensorFlow2 數(shù)據(jù)操作基礎(chǔ)
概述
TensorFlow2 的基本操作和 Numpy 的操作很像. 今天帶大家來看一看 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, 步長- 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, 會把它加入到 GraphKeys.TRAINABLE_VARIABLES, 才能對它使用 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)建一個所有參數(shù)為 0 的 tensor 對象. 類似于 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)建一個所有參數(shù)為 1 的 tensor 對象.
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)建一個與給定 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)建一個指定形狀和內(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. 里面的每一個元素大小必須在 [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 時的備選方案- 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.]]


Python“寶藏級”公眾號【Python之王】專注于Python領(lǐng)域,會爬蟲,數(shù)分,C++,tensorflow和Pytorch等等。
近 2年共原創(chuàng) 100+ 篇技術(shù)文章。創(chuàng)作的精品文章系列有:
日常收集整理了一批不錯的 Python 學(xué)習(xí)資料,有需要的小伙可以自行免費領(lǐng)取。
獲取方式如下:公眾號回復(fù)資料。領(lǐng)取Python等系列筆記,項目,書籍,直接套上模板就可以用了。資料包含算法、python、算法小抄、力扣刷題手冊和 C++ 等學(xué)習(xí)資料!
