The role of as_op is to convert a numerical function into a symbolic Op. You don't need @as_op inside perform method.
On Thursday, June 29, 2017 at 9:00:43 PM UTC+8, Giuseppe Angora wrote: > > I need to convert the 'scipy.distance.cdist' in a theano operation. I > start using theano 'as_op': > > @as_op(itypes=[theano.tensor.dmatrix, theano.tensor.dmatrix], otypes=[ > theano.tensor.dmatrix])#, infer_shape=infer_shape_numpy) > def scipy_cdist(X, W): > dist = distance.cdist(X, W, 'euclidean') > return dist > > > > It works but i can not evalute the gradient because no grad method is > defined, so i'm trying to create a new operation follow the theano > tutorial > <http://deeplearning.net/software/theano/extending/extending_theano.html>: > > from scipy.spatial import distanceimport theanoimport theano.tensor as T > class Scipy_cdist(theano.Op): > __props__ = () > > def __init__(self): > super(Scipy_cdist, self).__init__() > > def make_node(self, X, W): > X = theano.tensor.as_tensor_variable(X) > W = theano.tensor.as_tensor_variable(W) > return theano.Apply(self, [X, W], [X.type(), W.type()]) > > def perform(self, node, inputs, output_storage): > @as_op(itypes=[theano.tensor.dmatrix, theano.tensor.dmatrix], > otypes=[theano.tensor.dmatrix]) > def cdist(X, W): > dist = distance.cdist(X, W, 'euclidean') > return dist > X, W = inputs[0], inputs[1] > z = output_storage[0] > z[0] = cdist(X, W) > > > def infer_shape(self, node, input_shapes): > return input_shapes > > def grad(self, inputs, output_grads): > return [self.cdist(output_grads[0], output_grads[1])] > > > but there is some errors: > > cdist=Scipy_cdist() > d=cdist(X, W) > d[Scipy_cdist.0, Scipy_cdist.1] > > as you can see, the operation return me a list (this is the first error), > and if i try to eval: > > import numpy as np > XX=np.zeros((4,2)) > XX[1]+=[1,1] > XX[2]+=[0,1] > XX[3]+=[1,0] > > WW=np.zeros((3,2)) > WW[0]+=[0,3] > WW[1]+=[2,0] > WW[2]+=[4,4] > > d[1].eval({X:XX,W:WW})Traceback (most recent call last): > File > "C:\ProgramData\Anaconda3\lib\site-packages\theano\compile\function_module.py", > line 884, in __call__ > self.fn() if output_subset is None else\ > File "C:\ProgramData\Anaconda3\lib\site-packages\theano\gof\op.py", line > 891, in rval > r = p(n, [x[0] for x in i], o) > File "C:\Users\whyno\Documents\dNN\CNNtest\cNN_vsII\useful_op.py", line 88, > in perform > z[0] = cdist(X, W) > File "C:\ProgramData\Anaconda3\lib\site-packages\theano\gof\op.py", line > 615, in __call__ > node = self.make_node(*inputs, **kwargs) > File "C:\ProgramData\Anaconda3\lib\site-packages\theano\gof\op.py", line > 982, in make_node > if not all(inp.type == it for inp, it in zip(inputs, self.itypes)): > File "C:\ProgramData\Anaconda3\lib\site-packages\theano\gof\op.py", line > 982, in <genexpr> > if not all(inp.type == it for inp, it in zip(inputs, > self.itypes)):AttributeError: 'numpy.ndarray' object has no attribute 'type' > > > There is way to fix it? > -- --- You received this message because you are subscribed to the Google Groups "theano-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.