请问,有没有什么有效的方法可以在theano中提取隐藏层的数值?我可以使用 .eval() 来完成这项工作,这非常慢。
代码是(基于对dA.py的修改)
y = numpy.ndarray([n_train_batches*batch_size,n_hidden])
for batch_index in xrange(n_train_batches*batch_size):
input = train_set_x[batch_index]
start_time = timeit.default_timer()
newyeval = da.get_hidden_values(input)
end_time1 = timeit.default_timer()
newy = newyeval.eval()#da.get_hidden_values(input).eval(), I split here to test time
end_time2 = timeit.default_timer()
print 'get_hidden time: ', str(end_time1 - start_time)
print 'eval() time: ', str(end_time2 - end_time1)
事实证明 .eval() 超级慢,无论如何我可以改进它吗?我尝试用 theano.function 编写它,但我失败了。这是我的代码
## method3: function
WW = T.fmatrix()
bb = T.fmatrix()
inputs = T.fmatrix()
# print '---->503', type(WW), type(bb)
WW = T.cast(WW, dtype = 'floatX')
bb = T.cast(bb, dtype = 'floatX')
## f = theano.function([input, WW, bb],T.nnet.sigmoid(T.dot(input, WW) + bb) )
output = T.nnet.sigmoid(T.dot(inputs, WW) + bb)
inputArray = [inputs, WW, bb]
f = theano.function(inputArray, output)
start_time = timeit.default_timer()
newyeval = da.get_hidden_values(input)
y[batch_index] = f(input, da.W.get_value(), da.b.get_value())# I also tried the following, neither works, with same error log
#y[batch_index] = f(input, da.W, da.b)
end_time = timeit.default_timer()
print 'eval2() time: ', str(end_time - start_time)
它不起作用,错误日志是
Traceback (most recent call last):
File "src/dA_bo.py", line 598, in <module>
n_hidden = int(sys.argv[5]), corruption_level = float(sys.argv[6]) )
File "src/dA_bo.py", line 513, in test_dA
y[batch_index] = f(input, da.W, da.b)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 534, in __call__
allow_downcast=s.allow_downcast)
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/type.py", line 77, in filter
'Expected an array-like object, but found a Variable: '
TypeError: ('Bad input argument to theano function with name "src/dA_bo.py:507" at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')
所以我在这里有两个问题:
- 这里的功能代码怎么写?
- 有没有什么有效的方法可以输出theano中隐藏层的数值?