Python | método tensorflow.bitcast()

TensorFlow es una biblioteca de Python de código abierto diseñada por Google para desarrollar modelos de aprendizaje automático y redes neuronales de aprendizaje profundo. 

bitcast() es un método en la biblioteca tensorflow que se usa para transmitir un tensor de un tipo a otro tipo. No copia los datos.

Syntax:
tf.bitcast(
    input, type, name
)

Arguments: 
1. input: It is the Tensor and the allowed type for this tensor are
          bfloat16, half, float32, float64, int64, int32, uint8, uint16, uint32,
          uint64, int8, int16, complex64, complex128, qint8, quint8, qint16, quint16, qint32.
2. type: It defines the dtype in which input need to be bitcasted.
3. name: It is an optional argument. It is used to give a name to operation.
 
Return: It returns a tensor of type type.

Nota: bitcast no se puede usar para convertir dtype real en dtype complejo. Provocará InvalidArgumentError.

Ejemplo 1:

Python3

# importing the library
import tensorflow
  
# initializing the constant tensor of dtype unit32
a = tensorflow.constant(0xffffffff, dtype=tensorflow.uint32)
  
# Checking the initialized tensor
print('a:',a)
  
# bitcasting to dtype unit8
b = tensorflow.bitcast(a, tensorflow.uint8)
  
# Checking the bitcasted tensor
print('b:',b)

Producción:

a: tf.Tensor(4294967295, shape=(), dtype=uint32)
b: tf.Tensor([255 255 255 255], shape=(4,), dtype=uint8)

Ejemplo 2: 

Este ejemplo intenta convertir un dtype real en un dtype complejo

Python3

# importing the library
import tensorflow
  
# initializing the constant tensor of dtype unit32
a = tensorflow.constant(0xffffffff, dtype=tensorflow.uint32)
  
# Checking the initialized tensor
print('a:',a)
  
# bitcasting to dtype complex128
b = tensorflow.bitcast(a, tensorflow.complex128)

Producción:

Publicación traducida automáticamente

Artículo escrito por aman neekhara y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *