Python | Método de registro de Tensorflow()

Tensorflow es una biblioteca de aprendizaje automático de código abierto desarrollada por Google. Una de sus aplicaciones es desarrollar redes neuronales profundas. 
El módulo tensorflow.math brinda soporte para muchas operaciones matemáticas básicas. La función tf.log() [alias tf.math.log] brinda soporte para la función logarítmica natural en Tensorflow. Espera la entrada en forma de números complejos  $a+bi$  o números de coma flotante. El tipo de entrada es tensor y si la entrada contiene más de un elemento, se calcula un logaritmo por elementos,  y=\log_e x}$  .

Sintaxis : tf.log(x, nombre=Ninguno) o tf.math.log(x, nombre=Ninguno)
Parámetros
x : Un tensor de tipo bfloat16, half, float32, float64, complex64 o complex128. 
nombre (opcional): el nombre de la operación.
Tipo de retorno : un tensor con el mismo tamaño y tipo que el de x. 
 

Código #1: 

Python3

# Importing the Tensorflow library
import tensorflow as tf
 
# A constant vector of size 5
a = tf.constant([-0.5, -0.1, 0, 0.1, 0.5], dtype = tf.float32)
 
# Applying the log function and
# storing the result in 'b'
b = tf.log(a, name ='log')
 
# Initiating a Tensorflow session
with tf.Session() as sess:
    print('Input type:', a)
    print('Input:', sess.run(a))
    print('Return type:', b)
    print('Output:', sess.run(b))

Producción: 

Input type: Tensor("Const:0", shape=(5, ), dtype=float32)
Input: [-0.5 -0.1  0.   0.1  0.5]
Return type: Tensor("log:0", shape=(5, ), dtype=float32)
Output: [       nan        nan       -inf -2.3025851 -0.6931472]

$ nan $   denotes that natural logarithm doesn’t exist for negative values and $ -inf $   denotes that it approaches to negative infinity as the input approaches zero.
Code #2: Visualization 

Python3

# Importing the Tensorflow library
import tensorflow as tf
 
# Importing the NumPy library
import numpy as np
 
# Importing the matplotlib.pyplot function
import matplotlib.pyplot as plt
 
# A vector of size 20 with values from 0 to 1 and 1 to 10
a = np.append(np.linspace(0, 1, 10), np.linspace(1, 10, 10))
 
# Applying the logarithmic function and
# storing the result in 'b'
b = tf.log(a, name ='log')
 
# Initiating a Tensorflow session
with tf.Session() as sess:
    print('Input:', a)
    print('Output:', sess.run(b))
    plt.plot(a, sess.run(b), color = 'red', marker = "o")
    plt.title("tensorflow.abs")
    plt.xlabel("X")
    plt.ylabel("Y")
    plt.grid()
 
    plt.show()

Producción: 
 

Input: [ 0.          0.11111111  0.22222222  0.33333333  0.44444444  0.55555556
  0.66666667  0.77777778  0.88888889  1.          1.          2.
  3.          4.          5.          6.          7.          8.
  9.         10.        ]
Output: [       -inf -2.19722458 -1.5040774  -1.09861229 -0.81093022 -0.58778666
 -0.40546511 -0.25131443 -0.11778304  0.          0.          0.69314718
  1.09861229  1.38629436  1.60943791  1.79175947  1.94591015  2.07944154
  2.19722458  2.30258509]

Publicación traducida automáticamente

Artículo escrito por sanskar27jain 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 *