首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Theano计算forward_propagation

使用Theano计算forward_propagation
EN

Stack Overflow用户
提问于 2014-05-14 13:57:07
回答 1查看 1.1K关注 0票数 0

我试着用提亚诺编码前向传播。我将类名hiddenLayer定义如下:

将theano.tensor作为T从theano导入共享导入numpy作为np从theano导入函数

代码语言:javascript
复制
class hiddenLayer():
    """ Hidden Layer class
    """
    def __init__(self, n_in, n_out):
        rng = np.random
        self.W = shared(np.asarray(rng.uniform(low=-np.sqrt(6. / (n_in + n_out)),
                                               high=np.sqrt(6. / (n_in + n_out)),
                                               size=(n_in, n_out)),
                                   dtype=T.config.floatX),
                        name='W')
        self.b = shared(np.zeros(n_out, dtype=T.config.floatX), name='b')
        self.x = T.dvector('x')
        self.a = T.tanh(T.dot(self.x, self.W) + self.b)
        self.W_sum = shared(np.zeros([n_in, n_out]), name='W_sum')
        self.gw = 0
        self.gb = 0

我想设置一个hiddenLayer对象列表,当前的hiddenLayer是下一个hiddenLayer的输入。最后,我定义了一个名为forward的函数,它引发错误,代码如下所示:

代码语言:javascript
复制
def init_network(n_in, n_out, sl, x, y):
    l = []
    for i in range(sl):
        l.append(hiddenLayer(n_in, n_out))
    for i in range(sl):
        if i == 0:
            l[i].x = x
        elif i < sl-1:
            l[i].x = l[i-1].a
        else:
            l[i].x = l[i-1].a
            y = l[i].a
    return x, y, l

x = T.dvector('x')
y = T.dvector('y')
x, y, l = init_network(3, 3, 3, x, y)
forward = function(inputs=[x], outputs=y)

错误信息是:

代码语言:javascript
复制
theano.compile.function_module.UnusedInputError: theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index 0 is not part of the computational graph needed to compute the outputs: x.
To make this error into a warning, you can pass the parameter on_unused_input='warn' to theano.function. To disable it completely, use on_unused_input='ignore'.

你能告诉我为什么有什么问题以及如何解决吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-05-14 17:29:04

问题是,您在第二个循环中重写了l.x。你不能这么做。一旦在init中使用了Sel.x,基于它的结果将基于Sel.x的当前实例。所以当你覆盖它的时候,它不会在新的x上重新创建其他的东西。

您应该将x作为输入传递给init。如果没有,就创建一个。这是第一层。另一方面,它应该是前一层的输出。

代码语言:javascript
复制
def __init__(self, n_in, n_out, x=None):
   if x is not None:
      self.x = x
   else:
      x = T.dvector('x')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23656777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档