theano-valor de impresión de TensorVariable


¿Cómo puedo imprimir el valor numérico de una TensorVariable theano? Soy nuevo en theano, así que por favor sea paciente:)

Tengo una función donde obtengo y como parámetro. Ahora quiero depurar-imprimir la forma de este y a la consola. Usando

print y.shape

Resulta en la salida de la consola (estaba esperando números, i. e.(2,4,4)):

Shape.0

O cómo puedo imprimir el resultado numérico de, por ejemplo, el siguiente código (esto cuenta cuántos valores en y son más grandes superior a la mitad del máximo):

errorCount = T.sum(T.gt(T.abs_(y),T.max(y)/2.0))

errorCount debe ser un solo número porque T.sum resume todos los valores. Pero usando

print errCount

Me da (esperado algo como 134):

Sum.0
Author: SailAvid, 2013-07-03

4 answers

Si y es una variable theano, y.shape será una variable theano. así que es normal que

print y.shape

Retorno:

Shape.0

Si desea evaluar la expresión y. shape, puede hacer:

y.shape.eval()

Si y.shape no ingrese para calcularse a sí mismo(depende solo de la variable y constante compartidas). De lo contrario, si y depende de la variable x Theano, puede pasar el valor de entrada de la siguiente manera:

y.shape.eval(x=numpy.random.rand(...))

Esto es lo mismo para el sum. Theano graph son variables simbólicas que no hacen cómputo hasta que lo compilas con theano.function o llamas eval() en ellos.

EDITAR: Según los documentos , la sintaxis en las versiones más recientes de theano es

y.shape.eval({x: numpy.random.rand(...)})
 36
Author: nouiz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-29 15:35:12

Para futuros lectores: la respuesta anterior es bastante buena. Pero encontré la etiqueta.test_value ' mecanismo más beneficioso para fines de depuración (ver theano-debug-faq):

from theano import config
from theano import tensor as T
config.compute_test_value = 'raise'
import numpy as np    
#define a variable, and use the 'tag.test_value' option:
x = T.matrix('x')
x.tag.test_value = np.random.randint(100,size=(5,5))

#define how y is dependent on x:
y = x*x

#define how some other value (here 'errorCount') depends on y:
errorCount = T.sum(y)

#print the tag.test_value result for debug purposes!
errorCount.tag.test_value

Para mí, esto es mucho más útil; por ejemplo, verificar las dimensiones correctas, etc.

 13
Author: zuuz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-30 08:54:07

Valor de impresión de una variable Tensora.

Haga lo siguiente:

print tensor[dimension].eval() # esto imprimirá el contenido/valor en esa posición en el Tensor

Ejemplo, para un tensor 1d:

print tensor[0].eval()
 1
Author: Chandan Maruthi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-01 10:56:24

Use theano.printing.Print para agregar el operador de impresión a su gráfico computacional.

Ejemplo:

import numpy
import theano

x = theano.tensor.dvector('x')

x_printed = theano.printing.Print('this is a very important value')(x)

f = theano.function([x], x * 5)
f_with_print = theano.function([x], x_printed * 5)

#this runs the graph without any printing
assert numpy.all( f([1, 2, 3]) == [5, 10, 15])

#this runs the graph with the message, and value printed
assert numpy.all( f_with_print([1, 2, 3]) == [5, 10, 15])

Salida:

this is a very important value __str__ = [ 1. 2. 3.]

Fuente: Theano 1.0 docs: "¿Cómo puedo Imprimir un Valor Intermedio en una Función?"

 0
Author: Nicolas Ivanov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-05 13:32:34