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


Python Tensorflow bitwise.left_shift()用法及代碼示例

張量流bitwise.left_shift()方法對輸入b定義的輸入a執行left_shift操作,並返回新常數。該操作在a和b的表示上完成。
該方法屬於按位模塊。

用法:tf.bitwise.left_shift( a, b, name=None)

爭論

  • a:它必須是張量,應該來自以下類型之一:int8,int16,int32,int64,uint8,uint16,uint32,uint64。
  • b:這也應該是張量,類型與a相同。
  • 名稱:這是可選參數,這是操作的名稱。

返回:它返回與a和b具有相同類型的Tensor。

讓我們借助幾個示例來了解這個概念:
範例1:

import tensorflow as tf  
  
# A constant a and b 
a = tf.constant(8, dtype = tf.int32) 
b = tf.constant(2, dtype = tf.int32)   
  
# Applying the left_shift() function  
# storing the result in 'c'  
c = tf.bitwise.left_shift(a, b)  
  
# Initiating a Tensorflow session  
with tf.Session() as sess:
    print("Input 1", a) 
    print(sess.run(a)) 
    print("Input 2", b) 
    print(sess.run(b)) 
    print("Output:", c) 
    print(sess.run(c))

輸出:

Input 1 Tensor("Const_49:0", shape=(), dtype=int32)
8
Input 2 Tensor("Const_50:0", shape=(), dtype=int32)
2
Output: Tensor("LeftShift_1:0", shape=(), dtype=int32)
32

範例2:

import tensorflow as tf  
  
# A constant a and b 
a = tf.constant([8, 16, 32], dtype = tf.int32) 
b = tf.constant([2, 2, 3], dtype = tf.int32)   
  
# Applying the left_shift() function  
# storing the result in 'c'  
c = tf.bitwise.left_shift(a, b)  
  
# Initiating a Tensorflow session  
with tf.Session() as sess:
    print("Input 1", a) 
    print(sess.run(a)) 
    print("Input 2", b) 
    print(sess.run(b)) 
    print("Output:", c) 
    print(sess.run(c))

輸出:

Input 1 Tensor("Const_51:0", shape=(3, ), dtype=int32)
[ 8 16 32]
Input 2 Tensor("Const_52:0", shape=(3, ), dtype=int32)
[2 2 3]
Output: Tensor("LeftShift_2:0", shape=(3, ), dtype=int32)
[ 32  64 256]



相關用法


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