當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tensorflow.math.reduce_euclidean_norm()用法及代碼示例

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。

reduce_euclidean_norm()用於計算張量維度上的元素的歐幾裏得範數。

用法:tensorflow.math.reduce_euclidean_norm( input_tensor, axis, keepdims, name)

參數:

  • input_tensor:是減少的數字張量。
  • axis(optional):它表示要縮小的尺寸。其值應在[-rank(input_tensor),rank(input_tensor))範圍內。如果對此沒有給出值,則所有尺寸都會減小。
  • keepdims(optional):默認值為False。如果將其設置為True,它將保留長度為1的縮小尺寸。
  • name(optional):它定義了操作的名稱。

返回值:它返回一個張量。



範例1:

Python3

# importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
a = tf.constant([1, 2, 3, 4], dtype = tf.float64) 
  
# Printing the input tensor 
print('Input:', a) 
  
# Calculating result 
res = tf.math.reduce_euclidean_norm(a) 
  
# Printing the result 
print('Result:', res)

輸出:

Input: tf.Tensor([1. 2. 3. 4.], shape=(4, ), dtype=float64)
Result: tf.Tensor(5.477225575051661, shape=(), dtype=float64)



範例2:

Python3

# importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
a = tf.constant([[1, 2], [3, 4]], dtype = tf.float64) 
  
# Printing the input tensor 
print('Input:', a) 
  
# Calculating result 
res = tf.math.reduce_euclidean_norm(a, axis = 1, keepdims = True) 
  
# Printing the result 
print('Result:', res)

輸出:

Input: tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float64)
Result: tf.Tensor(
[[2.23606798]
 [5.        ]], shape=(2, 1), dtype=float64)



相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.math.reduce_euclidean_norm()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。