假设我有一个数组p = [ 0.27, 0.23, 0.1, 0.15, 0.2 ,0.05]
。设p
是随机变量X
的概率质量函数。现在,我正在编写一个theano代码,其中我在每次迭代中生成一个p
,我还有一个n
权重矩阵。(这里是[n = 6]
。)
现在,在每一次迭代中,我想选择其中一个权重矩阵来进一步传播。有人能帮我写这段代码吗。我不确定我是否可以编写正确的代码来启用反向传播(也就是说,正确地校正渐变)
注意,所有的W_i
以及输入p
都是模型参数。在
Edit
W1,W2,W3,W4,W5,W6,x,eps = T.dmatrices("W1","W2","W3","W4","W5","W6","x","eps")
b1,b2,b3,b4,b5,b6,pi = T.dcols("b1","b2","b3","b4","b5","b6","pi")
h_encoder = T.tanh(T.dot(W1,x) + b1)
rng = T.shared_randomstreams.RandomStreams(seed=124)
i = rng.choice(size=(1,), a=self.num_model, p=T.nnet.softmax(pi))
mu_encoder = T.dot(W2[i[0]*self.dimZ:(1+i[0])*self.dimZ].nonzero(),h_encoder) + b2[i[0]*self.dimZ:(1+i[0])*self.dimZ].nonzero()
log_sigma_encoder = (0.5*(T.dot(W3[i[0]*self.dimZ:(1+i[0])*self.dimZ].nonzero(),h_encoder)))+ b3[i[0]*self.dimZ:(1+i[0])*self.dimZ].nonzero()
z = mu_encoder + T.exp(log_sigma_encoder)*eps`
我的梯度变量是gradvariables = [W1,W2,W3,W4,W5,b1,b2,b3,b4,b5,pi]
忽略其他变量,因为它们是在其他地方定义的。现在,我得到以下错误
Traceback (most recent call last): File "trainmnist_mixture.py", line 55, in encoder.createGradientFunctions()
File "/home/amartya/Variational-Autoencoder/Theano/VariationalAutoencoder_mixture.py", line 118, in createGradientFunctions derivatives = T.grad(logp,gradvariables)
File "/usr/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py", line 543, in grad grad_dict, wrt, cost_name)
File "/usr/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py", line 1273, in _populate_grad_dict rval = [access_grad_cache(elem) for elem in wrt]
File "/usr/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py", line 1233, in access_grad_cache term = access_term_cache(node)[idx]
File "/usr/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py", line 944, in access_term_cache output_grads = [access_grad_cache(var) for var in node.outputs]
File "/usr/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/gradient.py", line 1243, in access_grad_cache term.type.why_null)
theano.gradient.NullTypeGradError: tensor.grad encountered a NaN. This variable is Null because the grad method for input 0 (Subtensor{int64:int64:}.0) of the Nonzero op is mathematically undefined
您可以使用
RandomStreams
实例的choice
方法。更多关于随机数的信息可以在文档here和here中找到。在下面是一个例子:
相关问题 更多 >
编程相关推荐