一小時(shí)學(xué)會(huì)TensorFlow2之基本操作1實(shí)例代碼
概述
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, 會(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ān)文章
python實(shí)現(xiàn)鄰接表轉(zhuǎn)鄰接矩陣
這篇文章主要介紹了python實(shí)現(xiàn)鄰接表轉(zhuǎn)鄰接矩陣,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
python中使用正則表達(dá)式的后向搜索肯定模式(推薦)
這篇文章主要介紹了python里使用正則表達(dá)式的后向搜索肯定模式,本文通過代碼介紹的非常詳細(xì),包括語法介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
Python如何使用隊(duì)列方式實(shí)現(xiàn)多線程爬蟲
這篇文章主要介紹了Python如何使用隊(duì)列方式實(shí)現(xiàn)多線程爬蟲,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
詳解如何使用Pandas創(chuàng)建有效且可復(fù)制的代碼
Pandas作為一種多功能和強(qiáng)大的工具而屹立不倒,其直觀的數(shù)據(jù)結(jié)構(gòu)和廣泛的功能使其成為無數(shù)數(shù)據(jù)專業(yè)人士和愛好者的首選,本文將使用Pandas創(chuàng)建有效且可復(fù)制的代碼,感興趣的可以了解下2024-11-11
python訓(xùn)練數(shù)據(jù)時(shí)打亂訓(xùn)練數(shù)據(jù)與標(biāo)簽的兩種方法小結(jié)
今天小編就為大家分享一篇python訓(xùn)練數(shù)據(jù)時(shí)打亂訓(xùn)練數(shù)據(jù)與標(biāo)簽的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python實(shí)現(xiàn)的簡單模板引擎功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的簡單模板引擎功能,結(jié)合具體實(shí)例形式分析了Python模版引擎的定義與使用方法,需要的朋友可以參考下2017-09-09
python實(shí)現(xiàn)csdn全部博文下載并轉(zhuǎn)PDF
我們學(xué)習(xí)編程,在學(xué)習(xí)的時(shí)候,會(huì)有想把有用的知識(shí)點(diǎn)保存下來,我們可以把知識(shí)點(diǎn)的內(nèi)容爬下來轉(zhuǎn)變成pdf格式,方便我們拿手機(jī)可以閑時(shí)翻看,是很方便的,本文就介紹一下如何實(shí)現(xiàn)2021-06-06

