0
$\begingroup$

Sorry but can anyone point out whats wrong with this code?

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())

The error given is:

ValueError: Input dimension mis-match. (input[0].shape[1] = 1, input[1].shape[1] = 3)
Apply node that caused the error: Elemwise{Composite{((i0 * i1) - (i2 * i3))}}(Subtensor{int64:int64:}.0, InplaceDimShuffle{x,0}.0, Subtensor{int64:int64:}.0, InplaceDimShuffle{x,0}.0)
Toposort index: 7
Inputs types: [TensorType(float64, matrix), TensorType(float64, row), TensorType(float64, matrix), TensorType(float64, row)]
Inputs shapes: [(3, 1), (1, 3), (3, 1), (1, 3)]
Inputs strides: [(8, 8), (24, 8), (8, 8), (24, 8)]
Inputs values: [array([[0.21068603],
       [0.73615298],
       [0.57416371]]), array([[0.95467599, 0.18011757, 0.12399378]]), array([[0.9940788 ],
       [0.27535941],
       [0.73075959]]), array([[ 0.0450856 , -0.16213227, -0.11161261]])]
Outputs clients: [[Elemwise{Composite{((i0 * i1 * i2) / i3)}}(TensorConstant{(1, 1) of 2.0}, Elemwise{Composite{((i0 * i1) - (i2 * i3))}}.0, InplaceDimShuffle{x,0}.0, Elemwise{mul,no_inplace}.0), Elemwise{Composite{((i0 * i1 * i2) / i3)}}(TensorConstant{(1, 1) of -2.0}, Elemwise{Composite{((i0 * i1) - (i2 * i3))}}.0, InplaceDimShuffle{x,0}.0, Elemwise{mul,no_inplace}.0), Elemwise{Sqr}[(0, 0)](Elemwise{Composite{((i0 * i1) - (i2 * i3))}}.0)]]

The problem is that I don't think I even have 4 inputs to begin with. By the way, the cost defined is not my real cost function. It has been simplified to illustrate the issue because the original one has the same issue. That is why there are many defined but unused variables. Thanks a lot!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.