一小時(shí)學(xué)會(huì)TensorFlow2之基本操作1實(shí)例代碼
概述
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ù)類(lèi)型
- shape: 表示生成常量數(shù)的維度
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 創(chuàng)建常量1 c1 = tf.constant(1) print(c1) # 創(chuàng)建常量, 類(lèi)型為bool c2 = tf.constant([True, False]) print(c2) # 創(chuàng)建常量1, 類(lèi)型為float32, 大小為3*3 c3 = tf.constant(0.1, shape=[2, 2]) print(c3) # 創(chuàng)建常量, 類(lèi)型為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: 開(kāi)始位置
- limit: 序列的上限
- delta: 相當(dāng)于 Numpy 的 step, 步長(zhǎng)
- detype: 數(shù)據(jù)類(lèi)型
- name: 數(shù)據(jù)名稱(chēng), 默認(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ù)名稱(chēng) | 參數(shù)類(lèi)型 | 參數(shù)含義 |
initial_value | 所有可以轉(zhuǎn)換為 Tensor 的類(lèi)型 | 變量的初始值 |
trainable | bool | 如果為 True, 會(huì)把它加入到 GraphKeys.TRAINABLE_VARIABLES, 才能對(duì)它使用 Optimizer |
collections | list | 指定該圖變量的類(lèi)型, 默認(rèn)為 [GraphKeys.GLOBAL_VARIABLES] |
validate_shape | bool | 如果為 False, 則不進(jìn)行類(lèi)型和維度檢查 |
name | string | 數(shù)據(jù)名稱(chēng) |
例子:
# 創(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ì)象. 類(lèi)似于 np.zeros.
格式:
tf.zeros(shape, dtype=tf.dtypes.float32, name=None)
參數(shù):
- shape: 數(shù)組的形狀
- dype: 數(shù)據(jù)類(lèi)型, 默認(rèn)為float32
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 創(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ù)類(lèi)型, 默認(rèn)為 float32
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 創(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 類(lèi)型大小一致的 tensor. 類(lèi)似 np.zeros_like.
格式:
tf.zeros_like(tensor, dype=None, name=None)
參數(shù):
- tensor: 傳入的 tensor
- dype: 數(shù)據(jù)類(lèi)型, 默認(rèn)為 float32
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 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ù)類(lèi)型, 默認(rèn)為 float32
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 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ù)名稱(chēng)
例子:
# 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ù)類(lèi)型, 默認(rèn)為 float32
- seed: 隨機(jī)數(shù)種子
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 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ù)類(lèi)型, 默認(rèn)為 float32
- seed: 隨機(jī)數(shù)種子
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 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ù)名稱(chēng)
例子:
# 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) # 類(lèi)似np.ndim
參數(shù):
- input: 傳入的張量
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 獲取張量維度 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ù)類(lèi)型
- dtype_hint: 當(dāng) dtype 為 None 時(shí)的備選方案
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 轉(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ù)類(lèi)型
格式:
cast(x, dtype, name=None)
參數(shù):
- x: 輸入的值
- dtype: 數(shù)據(jù)類(lèi)型
- name: 數(shù)據(jù)名稱(chēng)
例子:
# 裝換數(shù)據(jù)類(lèi)型 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)文章
Python3中bytes類(lèi)型轉(zhuǎn)換為str類(lèi)型
Python 3最重要的新特性之一是對(duì)字符串和二進(jìn)制數(shù)據(jù)流做了明確的區(qū)分。這篇文章主要介紹了Python3中bytes類(lèi)型轉(zhuǎn)換為str類(lèi)型的相關(guān)知識(shí),需要的朋友可以參考下2018-09-09Python開(kāi)發(fā)之快速搭建自動(dòng)回復(fù)微信公眾號(hào)功能
這篇文章主要介紹了Python開(kāi)發(fā)之快速搭建自動(dòng)回復(fù)微信公眾號(hào)功能的相關(guān)資料,需要的朋友可以參考下2016-04-04Python實(shí)現(xiàn)線性判別分析(LDA)的MATLAB方式
今天小編大家分享一篇Python實(shí)現(xiàn)線性判別分析(LDA)的MATLAB方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Django的用戶(hù)模塊與權(quán)限系統(tǒng)的示例代碼
這篇文章主要介紹了Django的用戶(hù)模塊與權(quán)限系統(tǒng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python3.5內(nèi)置模塊之os模塊、sys模塊、shutil模塊用法實(shí)例分析
這篇文章主要介紹了Python3.5內(nèi)置模塊之os模塊、sys模塊、shutil模塊用法,結(jié)合實(shí)例形式分析了Python os模塊、sys模塊及shutil模塊針對(duì)文件、路徑等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04python批量實(shí)現(xiàn)Word文件轉(zhuǎn)換為PDF文件
這篇文章主要為大家詳細(xì)介紹了python批量實(shí)現(xiàn)Word文件轉(zhuǎn)換為PDF文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03python光學(xué)仿真實(shí)現(xiàn)光線追跡折射與反射的實(shí)現(xiàn)
這篇文章主要為大家介紹了python光學(xué)仿真實(shí)現(xiàn)光線追跡折射與反射的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10python3使用騰訊企業(yè)郵箱發(fā)送郵件的實(shí)例
今天小編就為大家分享一篇python3使用騰訊企業(yè)郵箱發(fā)送郵件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06