Theano:获取矩阵的矩阵维和值(SharedVariable)

dis*_*ame 6 python theano

我想知道如何从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
Run Code Online (Sandbox Code Playgroud)

并且只返回

()
(1, 1)
Run Code Online (Sandbox Code Playgroud)

我也对打印/重新检索矩阵或向量的值感兴趣.

nou*_*uiz 14

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

w.get_value()
Run Code Online (Sandbox Code Playgroud)

然后这将工作:

w.get_value().shape
Run Code Online (Sandbox Code Playgroud)

但这将复制共享变量内容.要删除副本,您可以使用borrow参数,如下所示:

w.get_value(borrow=True).shape
Run Code Online (Sandbox Code Playgroud)

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

w.get_value(borrow=True, return_internal_type=True).shape
Run Code Online (Sandbox Code Playgroud)

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

w.shape.eval()
Run Code Online (Sandbox Code Playgroud)

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

如果您想了解更多关于Theano如何处理内存的信息,请访问以下网页:http://www.deeplearning.net/software/theano/tutorial/aliasing.html