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


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

<p> <a href=”https://www.geeksforgeeks.org/introduction-to-tensorflow/”> TensorFlow </a>是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。

clip_by_norm()用於將張量值裁剪為最大L2範數。

用法:tensorflow.clip_by_norm(t, clip_norm, axes, name)

參數:

  • t:需要裁剪的是輸入張量。
  • clip_norm:它是0-D標量張量,定義了最大限幅值。
  • axes(optional):它是一維矢量張量,定義了用於計算L2norm的尺寸。如果未提供任何尺寸,則將使用所有尺寸。
  • name(optional):它定義了操作的名稱。

返回值:



它返回張量。

範例1:

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
t = tf.constant([1, 2, 3, 4], dtype = tf.float64) 
clip_norm = .8
  
# Printing the input tensor 
print('t:', t) 
print('clip_norm:', clip_norm) 
  
# Calculating tangent 
res = tf.clip_by_norm(t, clip_norm) 
  
# Printing the result 
print('Result:', res)

輸出:

t: tf.Tensor([1. 2. 3. 4.], shape=(4, ), dtype=float64)
clip_norm: 0.8
Result: tf.Tensor([0.14605935 0.2921187  0.43817805 0.58423739], shape=(4, ), dtype=float64)

範例2:

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
t = tf.constant([1, 2, 3, 4], dtype = tf.float64) 
clip_norm = 5.2
  
  
# Printing the input tensor 
print('t:', t) 
print('clip_norm:', clip_norm) 
  
# Calculating tangent 
res = tf.clip_by_norm(t, clip_norm) 
  
# Printing the result 
print('Result:', res)

輸出:

t: tf.Tensor([1. 2. 3. 4.], shape=(4, ), dtype=float64)
clip_norm: 5.2
Result: tf.Tensor([0.94938577 1.89877153 2.8481573  3.79754307], shape=(4, ), dtype=float64)





相關用法


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