6

我想知道如何从 theano 中检索 SharedVariable 的维度。

这在这里例如不起作用:

from theano import *
from numpy import *

import numpy as np

w = shared( np.asarray(zeros((1000,1000)), np.float32) )

print np.asarray(w).shape
print np.asmatrix(w).shape

并且只返回

()
(1, 1)

我也有兴趣打印/检索矩阵或向量的值..

4

1 回答 1

14

您可以像这样获取共享变量的值:

w.get_value()

那么这将起作用:

w.get_value().shape

但这会复制共享变量的内容。要删除副本,您可以像这样使用借用参数:

w.get_value(borrow=True).shape

但是如果共享变量在 GPU 上,这仍然会将数据从 GPU 复制到 CPU。不要这样做:

w.get_value(borrow=True, return_internal_type=True).shape

他们是一种更简单的方法,编译一个返回形状的 Theano 函数:

w.shape.eval()

w.shape返回一个符号变量。.eval() 将编译一个 Theano 函数并返回 shape 的值。

如果您想了解有关 Theano 如何处理内存的更多信息,请查看此网页: http: //www.deeplearning.net/software/theano/tutorial/aliasing.html

于 2014-03-24T14:33:38.670 回答