Reputation: 404
I tried to code the forward propagation with Theano. I define a class name hiddenLayer as below:
import theano.tensor as T from theano import shared import numpy as np from theano import function
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
I want to set up a list of hiddenLayer objects and the current's hiddenLayer is the input of the next hiddenLayer. Finally I defined a function named forward buy it raises error and the code is as follow:
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)
The error message is:
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'.
Could you tell me why what's the problem with and how to solve it? Thanks
Upvotes: 0
Views: 1134
Reputation: 5071
The problem is that you override the l.x in the second loop. You can't do that. Once self.x is used in the init, the result based on it are based on the current instance of self.x. So when you override it, it don't recreate the other stuff on the new x.
You should pass x as in input to init. If None, create one. This is for the first layer. For the other, it should be the previous layer output.
def __init__(self, n_in, n_out, x=None):
if x is not None:
self.x = x
else:
x = T.dvector('x')
Upvotes: 1