Theano函数,可以在python中获取不同形状的输入数组

Theano函数,可以在python中获取不同形状的输入数组,python,function,input,theano,Python,Function,Input,Theano,在theano中,我想创建一个可以接受多个不同输入的函数,例如矩阵和向量 通常我会这样做: import theano import numpy x = theano.tensor.matrix(dtype=theano.config.floatX) y = 3*x f = theano.function([x],y) 但是,当我输入向量而不是矩阵时,例如: f(numpy.array([1,2,3])) 然后我得到一个尺寸不匹配的错误:“错误的尺寸数量:预期为2,形状为1(3,)。”

在theano中,我想创建一个可以接受多个不同输入的函数,例如矩阵和向量

通常我会这样做:

import theano
import numpy


x = theano.tensor.matrix(dtype=theano.config.floatX)
y = 3*x
f = theano.function([x],y)
但是,当我输入向量而不是矩阵时,例如:

f(numpy.array([1,2,3]))
然后我得到一个尺寸不匹配的错误:“错误的尺寸数量:预期为2,形状为1(3,)。”

有没有办法在theano中定义一个更通用的输入符号,它既可以接受矩阵,也可以接受不同形状的数组,如向量或三维数组,并且仍然有效


谢谢。

在编译Theano函数时,维度的数量必须固定。编译过程的一部分是根据维度的数量选择操作变量

你可以编译一个高维张量的函数,然后叠加你的输入,使它们具有所需的形状

所以

我们将接受和接受这些

f(numpy.array([[[1,2]]]))  # (1,1,3) vector wrapped as a tensor3
f(numpy.array([[[1,2],[3,4]]]))  # (1,2,2) matrix wrapped as a tensor3
f(numpy.array([[[1,2],[3,4]],[[5,6],[7,8]]]))  # (2,2,2) tensor3

谢谢!这正是我需要的答案,这样我就可以停止把时间花在寻找无法完成的事情上;)
f(numpy.array([[[1,2]]]))  # (1,1,3) vector wrapped as a tensor3
f(numpy.array([[[1,2],[3,4]]]))  # (1,2,2) matrix wrapped as a tensor3
f(numpy.array([[[1,2],[3,4]],[[5,6],[7,8]]]))  # (2,2,2) tensor3