在 theano 函数中传递一个 numpy 数组作为参数

passing numpy array as parameter in theano function

作为初学者,我尝试使用 theano 简单地计算两个矩阵的点积。

我的代码很简单

import theano
import theano.tensor as T
import numpy as np
from theano import function

def covarience(array):

    input_array=T.matrix('input_array')
    deviation_matrix = T.matrix('deviation_matrix')
    matrix_filled_with_1s=T.matrix('matrix_filled_with_1s')
    z = T.dot(input_array, matrix_filled_with_1s)

    identity=np.ones((len(array),len(array)))

    f=function([array,identity],z)
    # print(f)

covarience(np.array([[2,4],[6,8]]))

但问题是每次我 运行 此代码时,我都会收到类似 "TypeError: Unknown parameter type: "

的错误消息

谁能告诉我我的代码有什么问题?

不能将numpy数组传递给theano函数,theano functions只能由theano.tensor变量定义。因此,您始终可以通过 tensor/symbolic 变量的交互来定义计算,并且要对 values/real 数据执行实际计算,您可以使用函数,用 numpy 数组定义 theano 函数本身没有意义。

这应该有效:

import theano
import theano.tensor as T
import numpy as np

a = T.matrix('a')
b = T.matrix('b')
z = T.dot(a, b)
f = theano.function([a, b], z)
a_d = np.asarray([[2, 4], [6, 8]], dtype=theano.config.floatX)
b_d = np.ones(a_d.shape, dtype=theano.config.floatX)
print(f(a_d, b_d))