佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 一小時學會TensorFlow2之基本操作1實例代碼

一小時學會TensorFlow2之基本操作1實例代碼

熱門標簽:鐵路電話系統(tǒng) 銀行業(yè)務 美圖手機 檢查注冊表項 呼叫中心市場需求 網(wǎng)站文章發(fā)布 智能手機 服務器配置

概述

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

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

詳細講解一下 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)

輸出結果:

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: 相當于 Numpy 的 step, 步長
  • detype: 數(shù)據(jù)類型
  • name: 數(shù)據(jù)名稱, 默認為 “range”

例子:

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

輸出結果:

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 所有可以轉換為 Tensor 的類型 變量的初始值
trainable bool 如果為 True, 會把它加入到 GraphKeys.TRAINABLE_VARIABLES, 才能對它使用 Optimizer
collections list 指定該圖變量的類型, 默認為 [GraphKeys.GLOBAL_VARIABLES]
validate_shape bool 如果為 False, 則不進行類型和維度檢查
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

輸出結果:

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ù)類型, 默認為float32
  • name: 數(shù)據(jù)名稱

例子:

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

z2 = tf.zeros([3, 3])
print(z2)

輸出結果:

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ù)類型, 默認為 float32
  • name: 數(shù)據(jù)名稱

例子:

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

o2 = tf.ones([3, 3])
print(o2)

輸出結果:

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ù)類型, 默認為 float32
  • name: 數(shù)據(jù)名稱

例子:

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

輸出結果:

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ù)類型, 默認為 float32
  • name: 數(shù)據(jù)名稱

例子:

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

輸出結果:

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)

輸出結果:

[[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: 維度, 默認為 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)

輸出結果:

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)建隨機數(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)分布的均值, 默認為 0.0
  • stddev: 正態(tài)分布的標準差, 默認為 1.0
  • dtype: 數(shù)據(jù)類型, 默認為 float32
  • seed: 隨機數(shù)種子
  • name: 數(shù)據(jù)名稱

例子:

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

輸出結果:

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

均勻分布

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

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

參數(shù):

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

例子:

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

輸出結果:

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: 隨機數(shù)種子
  • name: 數(shù)據(jù)名稱

例子:

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

輸出結果:

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))

輸出結果:

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))

輸出結果:

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ù)轉換

轉換成張量

格式:

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

參數(shù):

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

例子:

# 轉換成張量
array = np.arange(6)
print(array.dtype)

array_tf = tf.convert_to_tensor(array)
print(array_tf)

輸出結果:

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

轉換數(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)

輸出結果:

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)

轉換成 numpy

例子:

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

輸出結果:

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

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

您可能感興趣的文章:
  • 一小時學會TensorFlow2之基本操作2實例代碼
  • 一小時學會TensorFlow2之全連接層

標簽:紅河 上海 沈陽 長治 滄州 樂山 新疆 河南

巨人網(wǎng)絡通訊聲明:本文標題《一小時學會TensorFlow2之基本操作1實例代碼》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    西平县| 曲阜市| 普兰店市| 剑河县| 冷水江市| 泰顺县| 如东县| 鹤壁市| 社旗县| 莱州市| 涞源县| 武义县| 阳西县| 新乐市| 靖江市| 茶陵县| 嘉义县| 七台河市| 邛崃市| 临漳县| 梁山县| 安仁县| 桃源县| 郴州市| 沂水县| 齐齐哈尔市| 西乡县| 周至县| 衡山县| 慈溪市| 巴彦淖尔市| 朝阳市| 墨玉县| 十堰市| 宁河县| 贺州市| 大丰市| 景洪市| 都兰县| 神池县| 鄱阳县|