Theano as_op error

I have a function I want to wrap using theano’s as_op. I have implemented as follows:

@tt.compile.ops.as_op(itypes=[tt.tensor.dscalar, tt.tensor.dvector, tt.tensor.dscalar],otypes=[tt.tensor.dvector, tt.tensor.dmatrix, tt.tensor.dmatrix])
def apply_params_rate(rate,params,npts_in):

This function is later called via

rates  = [1e-1, 1e0, 1e1, 1e2]
params = np.hstack([[50.,50.],[50., 50., 50.],[1e-1, 1e0, 1e1]])

npts = 100

...

for i,rate in enumerate(rates):
    out1,out2,out3 = apply_params_rate(rate,params,npts)

And I get the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-0e0e710b0e13> in <module>()
     23 for i,rate in enumerate(rates):
     24 
---> 25     out1,out2,out3 = apply_params_rate(rate,params,npts)
     26     time_storage[i,:],stress_storage[i,:,:],strain_storage[i,:,:] = apply_params_rate(rate,params,npts)
     27 

C:\Users\Natha\Anaconda2\lib\site-packages\theano\gof\op.pyc in __call__(self, *inputs, **kwargs)
    613         """
    614         return_list = kwargs.pop('return_list', False)
--> 615         node = self.make_node(*inputs, **kwargs)
    616 
    617         if config.compute_test_value != 'off':

C:\Users\Natha\Anaconda2\lib\site-packages\theano\gof\op.pyc in make_node(self, *inputs)
    961             raise ValueError("We expected %d inputs but got %d." %
    962                              (len(self.itypes), len(inputs)))
--> 963         if not all(inp.type == it for inp, it in zip(inputs, self.itypes)):
    964             raise TypeError(
    965                 "We expected inputs of types '%s' but got types '%s' " %

C:\Users\Natha\Anaconda2\lib\site-packages\theano\gof\op.pyc in <genexpr>((inp, it))
    961             raise ValueError("We expected %d inputs but got %d." %
    962                              (len(self.itypes), len(inputs)))
--> 963         if not all(inp.type == it for inp, it in zip(inputs, self.itypes)):
    964             raise TypeError(
    965                 "We expected inputs of types '%s' but got types '%s' " %

AttributeError: 'float' object has no attribute 'type'

Does anyone see where I’m going awry?

You declared the first input in your as_op function to have type tt.dscalar, but you give it a python float. You can convert python values to theano with tt.as_tensor_variable.

1 Like

Thank you! I was running into that wall for way too long.

1 Like