最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TensorFLow 數(shù)學運算的示例代碼

 更新時間:2020年04月21日 15:24:01   作者:蒼藍兒  
這篇文章主要介紹了TensorFLow 數(shù)學運算的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、Tensor 之間的運算規(guī)則

  • 相同大小 Tensor 之間的任何算術(shù)運算都會將運算應(yīng)用到元素級
  • 不同大小 Tensor(要求dimension 0 必須相同) 之間的運算叫做廣播(broadcasting)
  • Tensor 與 Scalar(0維 tensor) 間的算術(shù)運算會將那個標量值傳播到各個元素
  • Note: TensorFLow 在進行數(shù)學運算時,一定要求各個 Tensor 數(shù)據(jù)類型一致

二、常用操作符和基本數(shù)學函數(shù)

大多數(shù)運算符都進行了重載操作,使我們可以快速使用 (+ - * /) 等,但是有一點不好的是使用重載操作符后就不能為每個操作命名了。

# 算術(shù)操作符:+ - * / % 
tf.add(x, y, name=None)  # 加法(支持 broadcasting)
tf.subtract(x, y, name=None) # 減法
tf.multiply(x, y, name=None) # 乘法
tf.divide(x, y, name=None)  # 浮點除法, 返回浮點數(shù)(python3 除法)
tf.mod(x, y, name=None)  # 取余
 
# 冪指對數(shù)操作符:^ ^2 ^0.5 e^ ln 
tf.pow(x, y, name=None)  # 冪次方
tf.square(x, name=None)  # 平方
tf.sqrt(x, name=None)   # 開根號,必須傳入浮點數(shù)或復數(shù)
tf.exp(x, name=None)   # 計算 e 的次方
tf.log(x, name=None)   # 以 e 為底,必須傳入浮點數(shù)或復數(shù)
 
# 取符號、負、倒數(shù)、絕對值、近似、兩數(shù)中較大/小的
tf.negative(x, name=None)  # 取負(y = -x).
tf.sign(x, name=None)   # 返回 x 的符號
tf.reciprocal(x, name=None) # 取倒數(shù)
tf.abs(x, name=None)   # 求絕對值
tf.round(x, name=None)   # 四舍五入
tf.ceil(x, name=None)   # 向上取整
tf.floor(x, name=None)   # 向下取整
tf.rint(x, name=None)   # 取最接近的整數(shù) 
tf.maximum(x, y, name=None) # 返回兩tensor中的最大值 (x > y ? x : y)
tf.minimum(x, y, name=None) # 返回兩tensor中的最小值 (x < y ? x : y)
 
# 三角函數(shù)和反三角函數(shù)
tf.cos(x, name=None) 
tf.sin(x, name=None) 
tf.tan(x, name=None) 
tf.acos(x, name=None)
tf.asin(x, name=None)
tf.atan(x, name=None) 
 
# 其它
tf.div(x, y, name=None) # python 2.7 除法, x/y-->int or x/float(y)-->float
tf.truediv(x, y, name=None) # python 3 除法, x/y-->float
tf.floordiv(x, y, name=None) # python 3 除法, x//y-->int
tf.realdiv(x, y, name=None)
tf.truncatediv(x, y, name=None)
tf.floor_div(x, y, name=None)
tf.truncatemod(x, y, name=None)
tf.floormod(x, y, name=None)
tf.cross(x, y, name=None)
tf.add_n(inputs, name=None) # inputs: A list of Tensor objects, each with same shape and type
tf.squared_difference(x, y, name=None) 

三、矩陣數(shù)學函數(shù)

# 矩陣乘法(tensors of rank >= 2)
tf.matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, name=None)
 
# 轉(zhuǎn)置,可以通過指定 perm=[1, 0] 來進行軸變換
tf.transpose(a, perm=None, name='transpose')
 
# 在張量 a 的最后兩個維度上進行轉(zhuǎn)置
tf.matrix_transpose(a, name='matrix_transpose')
# Matrix with two batch dimensions, x.shape is [1, 2, 3, 4]
# tf.matrix_transpose(x) is shape [1, 2, 4, 3]
 
# 求矩陣的跡
tf.trace(x, name=None)

# 計算方陣行列式的值
tf.matrix_determinant(input, name=None)

# 求解可逆方陣的逆,input 必須為浮點型或復數(shù)
tf.matrix_inverse(input, adjoint=None, name=None)

# 奇異值分解
tf.svd(tensor, full_matrices=False, compute_uv=True, name=None)
 
# QR 分解
tf.qr(input, full_matrices=None, name=None)
 
# 求張量的范數(shù)(默認2)
tf.norm(tensor, ord='euclidean', axis=None, keep_dims=False, name=None)
 
# 構(gòu)建一個單位矩陣, 或者 batch 個矩陣,batch_shape 以 list 的形式傳入
tf.eye(num_rows, num_columns=None, batch_shape=None, dtype=tf.float32, name=None)
# Construct one identity matrix.
tf.eye(2)
==> [[1., 0.],
  [0., 1.]]
 
# Construct a batch of 3 identity matricies, each 2 x 2.
# batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2.
batch_identity = tf.eye(2, batch_shape=[3])
 
# Construct one 2 x 3 "identity" matrix
tf.eye(2, num_columns=3)
==> [[ 1., 0., 0.],
  [ 0., 1., 0.]]
 
# 構(gòu)建一個對角矩陣,rank = 2*rank(diagonal)
tf.diag(diagonal, name=None)
# 'diagonal' is [1, 2, 3, 4]
tf.diag(diagonal) ==> [[1, 0, 0, 0]
      [0, 2, 0, 0]
      [0, 0, 3, 0]
      [0, 0, 0, 4]]

# 其它
tf.diag_part
tf.matrix_diag
tf.matrix_diag_part
tf.matrix_band_part
tf.matrix_set_diag
tf.cholesky
tf.cholesky_solve
tf.matrix_solve
tf.matrix_triangular_solve
tf.matrix_solve_ls
tf.self_adjoint_eig
tf.self_adjoint_eigvals

四、Reduction:reduce various dimensions of a tensor

# 計算輸入 tensor 所有元素的和,或者計算指定的軸所有元素的和
tf.reduce_sum(input_tensor, axis=None, keep_dims=False, name=None)
# 'x' is [[1, 1, 1]
#   [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]] # 維度不縮減
tf.reduce_sum(x, [0, 1]) ==> 6
 
# 計算輸入 tensor 所有元素的均值/最大值/最小值/積/邏輯與/或
# 或者計算指定的軸所有元素的均值/最大值/最小值/積/邏輯與/或(just like reduce_sum)
tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_max(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_min(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_prod(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_all(input_tensor, axis=None, keep_dims=False, name=None) # 全部滿足條件
tf.reduce_any(input_tensor, axis=None, keep_dims=False, name=None) #至少有一個滿足條件

-------------------------------------------
# 分界線以上和 Numpy 中相應(yīng)的用法完全一致
-------------------------------------------
 
# inputs 為一 list, 計算 list 中所有元素的累計和,
# tf.add(x, y, name=None)只能計算兩個元素的和,此函數(shù)相當于擴展了其功能
tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None)

 
# Computes log(sum(exp(elements across dimensions of a tensor)))
tf.reduce_logsumexp(input_tensor, axis=None, keep_dims=False, name=None)
 
# Computes number of nonzero elements across dimensions of a tensor
tf.count_nonzero(input_tensor, axis=None, keep_dims=False, name=None)

五、Scan:perform scans (running totals) across one axis of a tensor

# Compute the cumulative sum of the tensor x along axis
tf.cumsum(x, axis=0, exclusive=False, reverse=False, name=None)
# Eg:
tf.cumsum([a, b, c]) # => [a, a + b, a + b + c]
tf.cumsum([a, b, c], exclusive=True) # => [0, a, a + b]
tf.cumsum([a, b, c], reverse=True) # => [a + b + c, b + c, c]
tf.cumsum([a, b, c], exclusive=True, reverse=True) # => [b + c, c, 0]
 
# Compute the cumulative product of the tensor x along axis
tf.cumprod(x, axis=0, exclusive=False, reverse=False, name=None)

六、Segmentation

沿著第一維(x 軸)根據(jù) segment_ids(list)分割好相應(yīng)的數(shù)據(jù)后再進行操作

# Computes the sum/mean/max/min/prod along segments of a tensor
tf.segment_sum(data, segment_ids, name=None)
# Eg:
m = tf.constant([5,1,7,2,3,4,1,3])
s_id = [0,0,0,1,2,2,3,3]
s.run(tf.segment_sum(m, segment_ids=s_id))
>array([13, 2, 7, 4], dtype=int32)
 
tf.segment_mean(data, segment_ids, name=None)
tf.segment_max(data, segment_ids, name=None)
tf.segment_min(data, segment_ids, name=None)
tf.segment_prod(data, segment_ids, name=None)
 
# 其它
tf.unsorted_segment_sum
tf.sparse_segment_sum
tf.sparse_segment_mean
tf.sparse_segment_sqrt_n

 七、 序列比較與索引提取

# 比較兩個 list 或者 string 的不同,并返回不同的值和索引
tf.setdiff1d(x, y, index_dtype=tf.int32, name=None) 
 
# 返回 x 中的唯一值所組成的tensor 和原 tensor 中元素在現(xiàn) tensor 中的索引
tf.unique(x, out_idx=None, name=None)
 
# x if condition else y, condition 為 bool 類型的,可用tf.equal()等來表示
# x 和 y 的形狀和數(shù)據(jù)類型必須一致
tf.where(condition, x=None, y=None, name=None) 
 
# 返回沿著坐標軸方向的最大/最小值的索引
tf.argmax(input, axis=None, name=None, output_type=tf.int64)
tf.argmin(input, axis=None, name=None, output_type=tf.int64)
 
# x 的值當作 y 的索引,range(len(x)) 索引當作 y 的值
# y[x[i]] = i for i in [0, 1, ..., len(x) - 1]
tf.invert_permutation(x, name=None)
 
# 其它
tf.edit_distance

到此這篇關(guān)于TensorFLow 數(shù)學運算的示例代碼的文章就介紹到這了,更多相關(guān)TensorFLow 數(shù)學運算內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python3.6利用pyinstall打包py為exe的操作實例

    python3.6利用pyinstall打包py為exe的操作實例

    今天小編就為大家分享一篇python3.6利用pyinstall打包py為exe的操作實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)的使用教程

    python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)的使用教程

    shape函數(shù)是numpy.core.fromnumeric中的函數(shù),它的功能是讀取矩陣的長度,比如shape[0]就是讀取矩陣第一維度的長度,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)使用的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 一文掌握Python描述符與裝飾器的神奇妙用

    一文掌握Python描述符與裝飾器的神奇妙用

    Python 是一種多范式編程語言,具有靈活的特性,其中可調(diào)用實例、嵌套函數(shù)、描述符和裝飾器是其功能強大的特性之一,這些概念對于編寫高效、優(yōu)雅的代碼至關(guān)重要
    2024-01-01
  • 如何利用python之wxpy模塊玩轉(zhuǎn)微信

    如何利用python之wxpy模塊玩轉(zhuǎn)微信

    這篇文章主要介紹了利用python之wxpy模塊玩轉(zhuǎn)微信,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Python?中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換問題

    Python?中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換問題

    這篇文章主要介紹了Python中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換,在Python中,我們可以使用內(nèi)置的hex()函數(shù)將10進制數(shù)轉(zhuǎn)換為16進制數(shù),需要的朋友可以參考下
    2023-05-05
  • python如何創(chuàng)建TCP服務(wù)端和客戶端

    python如何創(chuàng)建TCP服務(wù)端和客戶端

    這篇文章主要為大家詳細介紹了python如何創(chuàng)建TCP服務(wù)端和客戶端,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Python實現(xiàn)推送百度鏈接的示例代碼

    Python實現(xiàn)推送百度鏈接的示例代碼

    有時為了提高搜索效率,也讓搜索引擎更容易發(fā)現(xiàn)自己的文章,我們需要將文章鏈接推送到百度站長平臺,起到快速收錄的目的。本文將主要介紹如何通過Python實現(xiàn)這一功能,需要的可以參考一下
    2021-12-12
  • 如何用python刪除csv文件中的某幾列或行

    如何用python刪除csv文件中的某幾列或行

    這篇文章主要給大家介紹了關(guān)于如何用python刪除csv文件中的某幾列或行的相關(guān)資料,在Python中我們常常需要對csv文件進行操作,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • python可視化實現(xiàn)KNN算法

    python可視化實現(xiàn)KNN算法

    這篇文章主要為大家詳細介紹了python可視化實現(xiàn)KNN算法,通過繪圖工具Matplotlib包可視化實現(xiàn)機器學習中的KNN算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 使用Python OpenCV為CNN增加圖像樣本的實現(xiàn)

    使用Python OpenCV為CNN增加圖像樣本的實現(xiàn)

    這篇文章主要介紹了使用Python OpenCV為CNN增加圖像樣本的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06

最新評論

新绛县| 炎陵县| 佛教| 南宫市| 阳江市| 泸西县| 图们市| 漯河市| 龙岩市| 余江县| 昌乐县| 阿克苏市| 万安县| 永修县| 集安市| 基隆市| 南郑县| 泸溪县| 五原县| 汤阴县| 藁城市| 南召县| 芮城县| 东阳市| 察雅县| 定南县| 黄平县| 绍兴市| 滨州市| 武宁县| 广南县| 横山县| 安远县| 邛崃市| 南阳市| 武川县| 灵寿县| 金山区| 漳平市| 墨竹工卡县| 荔波县|