如何将 theano.tensor 切换到 numpy.array? [英] How to swich theano.tensor to numpy.array?

查看:28
本文介绍了如何将 theano.tensor 切换到 numpy.array?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所示的简单代码:

I have simple codes as shown below:

class testxx(object):
    def __init__(self, input):
        self.input = input
        self.output = T.sum(input)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32)
classfier = testxx(a)
outxx = classfier.output
outxx = np.asarray(outxx, dtype = np.float32)

但是,我收到以下错误信息:

However, I get the following error information:

ValueError: setting an array element with a sequence.

此外,当我使用theano.tensor的函数时,它返回的似乎是所谓的张量",我不能简单地将其切换为numpy.array类型,即使结果应该是什么形状一个矩阵.

Furthermore, when I use the function of theano.tensor, it seems that what it returns is called "tensor", and I can't simply switch it to the type numpy.array, even though what the result should shape like a matrix.

所以这就是我的问题:如何将 outxx 切换为 numpy.array 类型?

So that's my question:how can I switch outxx to type numpy.array?

推荐答案

Theano 张量"变量是符号变量.你用它们构建的东西就像你编写的程序.你需要编译一个 Theano 函数来执行这个程序的功能.有两种方法可以编译 Theano 函数:

Theano "tensor" variable are symbolic variable. What you build with them are like a programme that you write. You need to compile a Theano function to execute what this program do. There is 2 ways to compile a Theano function:

f = theano.function([testxx.input], [outxx])
f_a1 = f(a)

# Or the combined computation/execution
f_a2 = outxx.eval({testxx.input: a})

当你编译一个 Theano 函数时,你必须知道输入是什么,输出是什么.这就是为什么在对 theano.function() 的调用中有 2 个参数.eval() 是一个接口,它将在给定的符号输入和相应的值上编译和执行 Theano 函数.

When you compile a Theano function, your must tell what the input are and what the output are. That is why there is 2 parameter in the call to theano.function(). eval() is a interface that will compile and execute a Theano function on a given symbolic inputs with corresponding values.

这篇关于如何将 theano.tensor 切换到 numpy.array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆