受MLP Theano教程的启发,我有以下定义:
Weights = rng.uniform(low=low, high=high, size=(n_in, n_out))
W_values = numpy.asarray(Weights, dtype=theano.config.floatX)
W = theano.shared(value=W_values, name='W', borrow=True)
b_values = numpy.zeros((n_out,), dtype=theano.config.floatX)
b = theano.shared(value=b_values, name='b', borrow=True)
sigmas = rng.uniform(low=0.001, high=100, size=(n_out,))
s_values = numpy.asarray(sigmas, dtype=theano.config.floatX)
s = theano.shared(value=s_values, name='s', borrow=True)
input = T.matrix("input")
我想计算一个高斯激活,如下所示:
^{pr2}$然而,这是不可能的,因为W、X和s都不可编辑。如何高效地编写Theano版本?在
非常感谢。在
编辑:
我配置了一个解决方案:
# Declare an intermediate shared variable:
h_values = numpy.zeros((batch_s, n_out), dtype=theano.config.floatX)
dot_H = theano.shared(value=h_values, name='h', borrow=True)
# compute the kernel for an input vector and then for the whole batch
for i in range(batch_s):
dot_H=T.set_subtensor(dot_H[i],theano.scan(lambda w, sig, bias: \
T.exp(-ops.norm(w - input[i], 2) ** 2 / 2*sig ** 2)
+bias,
sequences=[self.W.T, self.s, self.b])[0])
# retrieve the solution as a numpy matrix
output = dot_H
但是。。我注意到,这将返回dot_H
中的所有项0。似乎连差w - input[i]
也不能正确计算。在
编辑2我已经解决了这个问题,但是我认为矿山不是最有效的解决方案,有人能给我推荐更好的吗?在
import theano.tensor as T
import numpy
import theano
batch_s=5
dims=10
hidd_s=3
out_s=2
missing_param = None #"ignore"
rng = numpy.random.RandomState(1234)
input = T.matrix("input")
X = numpy.asarray(rng.uniform(low=-2.1, high=5.0, size=(batch_s, dims)))
def layer(x):
W=theano.shared(
value=numpy.asarray(
rng.uniform(low=0.001, high=1.0, size=(dims, hidd_s)),
dtype=theano.config.floatX),
name='W', borrow=True)
S=theano.shared(
value=numpy.asarray(
rng.uniform(low=10.0, high=100.0, size=(hidd_s, )),
dtype=theano.config.floatX),
name='S', borrow=True)
dot_H = theano.shared(
value=numpy.zeros((batch_s, hidd_s),
dtype=theano.config.floatX),
name='dot_H', borrow=True)
for i in range(batch_s):
for j in range(hidd_s):
dot_H = T.set_subtensor(dot_H[i,j],
T.exp(-(W.T[j] - x[i]).norm(2) ** 2) / 2 * S[j] ** 2)
return dot_H
layer_out = theano.function(
inputs=[input],
outputs=layer(input),
on_unused_input=missing_param
)
print layer_out(X)
T.set_subtensor(x[...], y)
将返回一个“符号”变量,并将给定的子传感器替换为y
。它实际上不会在x
内部运行。要更改共享变量x
,您需要使用带有新值的theano.function
和“update
”x
。在相关问题 更多 >
编程相关推荐