Is there any function in Pytensor can convert np arrays to tensors

Is there any function in Pytensor can convert np arrays to tensors?Like np_array = np.array(data) x_np = torch.from_numpy(np_array)
in PyTorch?

pt.as_tensor(np_array) does that

Thanks.I have another question:
When I use normal likelihood,I need to determine mu by another function, and this function will be called in pymc.

with pm.Model() as poisson_model:
    base = pm.Uniform("base", lower=0.0, upper = 5.0)
    Tion = pm.Uniform('Tion',lower=0.5,upper=9.0)
    LogYield = pm.Uniform('LogYield',0,2)
    Yield = pm.Deterministic('Yield',10**(LogYield))
    y_observed = pm.Poisson(
        "y_observed",
        mu=Rates(E,Tion,base,Yield),
        observed=data
    )
    rates = pm.Deterministic('rates', Rates(E,Tion,base,Yield))
    prior = pm.sample_prior_predictive()
    posterior = pm.sample(draws = Samples,chains=4,cores=4)
    posterior_pred_poisson = pm.sample_posterior_predictive(posterior)

in mu, may I transform the variables to numpy array, and transform the result to Pytensor tensor and return to pymc module.

def ballabio(E,ion_temp):    
    print(ion_temp.type)
    a1 = np.array([5.30509, 2.4736e-3, 1.84, 1.3818])
    a2 = np.array([5.1068e-4, 7.6223e-3, 1.78, 8.7691e-5])
    Eo = 14.021*1e3
    wo = 177.259
    Ti = ion_temp
    dE = (a1[0]/( 1 + a1[1]*Ti**a1[2] ))*Ti**( 2.0/3.0 ) + a1[3]*Ti
    dw = (a2[0]/( 1 + a2[1]*Ti**a2[2] ))*Ti**( 2.0/3.0 ) + a2[3]*Ti
    Emean = Eo + dE
    Sth = wo*(1+dw)*pm.math.sqrt(Ti)/(2*pm.math.sqrt(2*pm.math.log(2)))

    Ebar = Emean*pm.math.sqrt(1.0-1.5*Sth/Emean**2.0)
    S = (4./3.)*Ebar*(Emean-Ebar)        
    f = pm.math.exp(-( 2*Ebar/S**2 ) * ( pm.math.sqrt(E)-pm.math.sqrt(Ebar))**2)
    g=f.eval()
    k=pt.as_tensor(g)
    return k/pm.math.sum(k)      

This is just an example, the function will be much more complicated ,I do this because I’m not familiar with pytensor, and the function will contain np.convolve.However,I tried in this example, but pymc can’t work well.
image
It should work like this:
image

No, you have to specify all operations with PyTensor functions. There are convolution functions in PyTensor that should behave similar to numpy, although the documentation is missing IIRC (cc @jessegrabowski)

There are two open issues with the convolution sub-module here and here. I thought there was one more asking for a function that exactly matched the signature on np.convolve, but I can’t seem to find it. Help definitely wanted.

I understand, maybe I have to write a function works the same as np, convolve,but I’m not familiar with it.The different between causal_conv1d in Pytensor and np.convolve is output’s scale, in causal_conv1d returns an tensor scale the same as input ,but in numpy it comes out the n+m-1,n is the scale of input array, m is the scale of filter.But if I full the input fill it with zeros to length m+n-1,like this, it will work the same as np.convolve, but I don’t know why:

A=np.array([1,2,3,4,5,6,7])
B=np.array([1,2,3,4,5,6,7,0,0])
C=np.array([5,6,7])
D=np.convolve(A,C)
E=conv.causal_conv1d(A[None,None,:],C[None,None,:],filter_shape=(None,None,3),input_shape=(None,None,9)).squeeze()
F=conv.causal_conv1d(B[None,None,:],C[None,None,:],filter_shape=(None,None,3)).squeeze()
G=np.zeros(9,)
print(D)
print(E.eval())
print(F.eval())
 
    >-[  5  16  34  52  70  88 106  84  49]
    >-[  5  16  34  52  70  88 106]
    >-[  5  16  34  52  70  88 106  84  49]

I only find these in the document about causal_conv1d.

def causal_conv1d(
    input,
    filters,
    filter_shape,
    input_shape=None,
    subsample=1,
    filter_flip=True,
    filter_dilation=1,
    num_groups=1,
    unshared=False,
):
    """
    Computes (dilated) causal convolution

    The output at time t depends only on the inputs till t-1. Used for
    modelling temporal data.
    See [WaveNet: A Generative Model for Raw Audio, section 2.1]
    (https://arxiv.org/abs/1609.03499).

    Parameters
    ----------
    input : symbolic 3D tensor
        mini-batch of feature vector stacks, of shape
        (batch_size, input_channels, input_length)
        See the optional parameter ``input_shape``
    filters : symbolic 3D tensor
        Set of filters used in the CNN, of shape
        (output_channels, input_channels, filter_length)
    filter_shape : [None/int/Constant] * 2 + [Tensor/int/Constant]
        The shape of the filters parameter.
        A tuple/list of len 3, with the first two dimensions
        being None or int or Constant and the last dimension being
        Tensor or int or Constant.
        Not optional, since the filter length is needed to calculate
        the left padding for causality.
    input_shape : None or [None/int/Constant] * 3
        The shape of the input parameter.
        None, or a tuple/list of len 3.
        Optional, possibly used to choose an optimal implementation.
    subsample : int
        The factor by which to subsample the output. Also called strides
        elsewhere.
    filter_dilation : int
        Factor by which to subsample (stride) the input. Also called
        dilation factor.
    num_groups : int
        Divides the image, kernel and output tensors into num_groups
        separate groups. Each which carry out convolutions separately
    unshared : bool
        If true, then unshared or 'locally connected' convolution will be
        performed. A different filter will be used for each region of the
        input.

    Returns
    -------
    Symbolic 3D tensor.
        Set of feature vectors generated by convolutional layer. Tensor is
        of shape (batch_size, output_channels, output_length)

    Notes
    -----

    :note: Currently, this is implemented with the 2D convolution ops.

    """

I don’t know which way I should choose, figure out a new function or just full the tensor length with zeros? Both way may be difficult for me. I don‘t know how to operate a fvector tensor like a numpy array, like how to concatenate two tensors like numpy.concatenate() or full a tensor with zeros to a specified length.I‘m stuck in this problem for few weeks, I desperately need help.

There is pt.concatenate for concatenation and pt.zeros for zeros. Most numpy functions have an equivalent in the pytensor.tensor module. You can check the api docs for exhaustive details (maybe it’s useful to search for specific numpy functions you are looking for equivalents to?), or you might benefit from the getting started tutorial series.

Thanks, if there a better equivalent function to np.covlove, better than causal_conv1d?

I don’t understand what you mean by “better”. If you mean “one that does the exact computation that I want”, I still suggest you use a scan, as I showed in this post. You will have 100% control over the output in this case.

Frankly though, it seems like your issues are just related to padding and slicing on the left and right of your data. You can likely accomplish what you want using conv1d_causal together with pt.concatenate. Here’s a gist showing how to accomplish every mode in numpy using slicing, padding, and conv1d_causal.

Thanks,I will try

I updated the post you marked as the solution with a notebook I think you will find useful.