Theano梯度下降和成本函数问题

时间:2019-09-23 16:55:58

标签: theano gradient-descent loss-function

我收到“ ValueError:输入尺寸不匹配”。用于我的theano梯度下降实施。

我尝试使用np.transpose转换输入,但是尺寸保持不变。

import theano
from theano import tensor as T
import numpy as np
import math 
import cmath 
global W
l=1

def inputvector(theta,phi,zeta):
    global xreal,ximag
    xreal=np.asarray([math.cos(2*theta-phi)*math.cos(phi), math.cos(2*theta-phi)*math.sin(phi)*math.cos(zeta), math.cos(2*theta-phi)*math.sin(phi)*math.sin(zeta)])
    ximag=np.asarray([-math.sin(2*theta-phi)*math.sin(phi), math.sin(2*theta-phi)*math.cos(phi)*math.cos(zeta), math.sin(2*theta-phi)*math.cos(phi)*math.sin(zeta)])   
    return xreal,ximag


def weights(w11r,w12r,w13r,w21r,w22r,w23r,w31r,w32r,w33r,w11i,w12i,w13i,w21i,w22i,w23i,w31i,w32i,w33i):
    global W
    W = theano.shared(np.asarray([w11r, w12r, w13r,w21r,w22r,w23r,w31r,w32r,w33r,w11i,w12i,w13i,w21i,w22i,w23i,w31i,w32i,w33i]), 'W')
    return W

xreal=T.dvector('xreal')
ximag=T.dvector('ximag')
weights(np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1),np.random.rand(1))

cost=T.mean(T.sqr(np.dot(W[0:3],xreal)-np.dot(W[9:12],ximag)))

gradients = theano.tensor.grad(cost, [W])
W_updated = W - (0.1 * gradients[0])
updates = [(W, W_updated)]
train=theano.function(inputs=[xreal,ximag],outputs=cost,updates=updates,allow_input_downcast=True)
for i in range(100):
    inputvector(np.random.rand(1),np.random.rand(1),np.random.rand(1))
    train(xreal,ximag)
    print (W.get_value())

我看不出输入大小不一致的任何原因。而且我不认为我什至有4个输入。

0 个答案:

没有答案