`salidas_capa3` is a theano function, which is a callable object.
However, you are trying to _index_ into it using `salidas_capa3[test_model(i)]`.
What is the behaviour you would expect from that code?

On Wed, Jul 27, 2016, Beatriz G. wrote:
> Hi guays, 
> 
> I am having a similar problem, I am trying to compile the following theano 
> function:
> 
> layer3 = LogisticRegression(input=layer2.output, n_in=100, n_out=4)
> 
> 
> salidas_capa3 = theano.function(
>     [index],
>     layer3.y_pred,
>     givens={
>         x: test_set_x[index * batch_size: (index + 1) * batch_size]
>     }
> )
> 
> 
> 
> Which I call in the test part:
> 
> 
> for i in range(n_test_batches):
>     test_losses = [test_model(i)]
>     y_pred_test = salidas_capa3[test_model(i)]
>     print y_pred_test
>     test_score = numpy.mean(test_losses)
> 
> 
> 
> And I am getting the next error:
> 
> 
> Traceback (most recent call last):
>   File "/home/beaa/Escritorio/Theano/Separando_Lenet.py", line 416, in 
> <module>
>     evaluate_lenet5()
>   File "/home/beaa/Escritorio/Theano/Separando_Lenet.py", line 392, in 
> evaluate_lenet5
>     y_pred_test = salidas_capa3[test_model(i)]
>   File 
> "/home/beaa/.local/lib/python2.7/site-packages/theano/compile/function_module.py",
>  line 545, in __getitem__
>     return self.value[item]
>   File 
> "/home/beaa/.local/lib/python2.7/site-packages/theano/compile/function_module.py",
>  line 480, in __getitem__
>     s = finder[item]
> TypeError: unhashable type: 'numpy.ndarray'
> 
> 
> 
> 
> Anyone could guide me?
> 
> 
> Here is my code:
> 
> 
> class LeNetConvPoolLayer(object):
>     """Pool Layer of a convolutional network """
> 
>     def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 
> 2)):
>         """
>         Allocate a LeNetConvPoolLayer with shared variable internal 
> parameters.
>         :type rng: numpy.random.RandomState
>         :param rng: a random number generator used to initialize weights
>         :type input: theano.tensor.dtensor4
>         :param input: symbolic image tensor, of shape image_shape
>         :type filter_shape: tuple or list of length 4
>         :param filter_shape: (number of filters, num input feature maps,
>                               filter height, filter width)
>         :type image_shape: tuple or list of length 4
>         :param image_shape: (batch size, num input feature maps,
>                              image height, image width)
>         :type poolsize: tuple or list of length 2
>         :param poolsize: the downsampling (pooling) factor (#rows, #cols)
>         """
> 
>         assert image_shape[1] == filter_shape[1]                            
> #El numero de feature maps sea igual en ambas variables
>         self.input = input
> 
>         # there are "num input feature maps * filter height * filter width"
>         # inputs to each hidden unit
>         fan_in = numpy.prod(filter_shape[1:])                                
> #el numero de neuronas en la capa anterio
>         # each unit in the lower layer receives a gradient from:
>         # "num output feature maps * filter height * filter width" /
>         #   pooling size
> 
>         fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /          
> #El numero de neuronas de salida: numero de filtros*alto*ancho del 
> filtro/poolsize(tam*tam)
>                    numpy.prod(poolsize))
>         # initialize weights with random weights
> 
>         # initialize weights with random weights
>         W_bound = numpy.sqrt(6. / (fan_in + fan_out))
>         self.W = theano.shared(
>             numpy.asarray(
>                 rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
>                 # Se calcula asi el W_bound por la funcion  de activacion 
> tangencial.
>                 # Los pesos dependen del tamanyo del filtro(neuronas)
> 
>                 dtype=theano.config.floatX  # Para que sea valido en gpu
>             ),
>             borrow=True
>         )
> 
>         # the bias is a 1D tensor -- one bias per output feature map
>         b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
>         self.b = theano.shared(value=b_values, borrow=True)
> 
>         # convolve input feature maps with filters
>         conv_out = conv.conv2d(
>             input=input,
>             filters=self.W,
>             filter_shape=filter_shape,
>             image_shape=image_shape
>         )
> 
>         # downsample each feature map individually, using maxpooling
>         pooled_out = downsample.max_pool_2d(
>             input=conv_out,
>             ds=poolsize,
>             ignore_border=True
>         )
> 
>         print pooled_out
> 
>         # add the bias term. Since the bias is a vector (1D array), we first
>         # reshape it to a tensor of shape (1, n_filters, 1, 1). Each bias will
>         # thus be broadcasted across mini-batches and feature map
>         # width & height
>         self.output = theano.tensor.nnet.relu((pooled_out + 
> self.b.dimshuffle('x', 0, 'x', 'x')) , alpha=0)
> 
> 
>         # store parameters of this layer
>         self.params = [self.W, self.b]
> 
>         # keep track of model input
>         self.input = input
> 
>         self.conv_out=conv_out
>         self.pooled_out=pooled_out
> 
>         self.salidas_capa = [self.conv_out, self.pooled_out, self.output]
> 
> 
> 
> def evaluate_lenet5(learning_rate=0.001, n_epochs=2, nkerns=[48, 96], 
> batch_size=20):
>     """ Demonstrates lenet on MNIST dataset
> 
>     :type learning_rate: float
>     :param learning_rate: learning rate used (factor for the stochastic
>                           gradient)
> 
>     :type n_epochs: int
>     :param n_epochs: maximal number of epochs to run the optimizer
> 
>     :type dataset: string
>     :param dataset: path to the dataset used for training /testing (MNIST 
> here)
> 
>     :type nkerns: list of ints
>     :param nkerns: number of kernels on each layer
>     """
> 
>     rng = numpy.random.RandomState(2509)
> 
>     train_set_x, test_set_x, train_set_y, test_set_y, valid_set_x, 
> valid_set_y =  Load_casia_Data2()
> 
>     train_set_x = theano.shared(numpy.array(train_set_x,  dtype='float64',))
>     test_set_x = theano.shared(numpy.array(test_set_x, dtype='float64'))
>     train_set_y = theano.shared(numpy.array(train_set_y,  dtype='int32'))
>     test_set_y = theano.shared(numpy.array(test_set_y, dtype='int32'))
>     valid_set_x = theano.shared(numpy.array(valid_set_x, dtype='float64'))
>     valid_set_y = theano.shared(numpy.array(valid_set_y, dtype='int32'))
> 
>     print("n_batches:")
> 
>     # compute number of minibatches for training, validation and testing
>     n_train_batches = train_set_x.get_value(borrow=True).shape[0]
>     n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
>     n_test_batches = test_set_x.get_value(borrow=True).shape[0]
>     print("n_train_batches: %d" % n_train_batches)
>     print("n_valid_batches: %d" % n_valid_batches)
>     print("n_test_batches: %d" % n_test_batches)
> 
>     n_train_batches /= batch_size
>     n_valid_batches /= batch_size
>     n_test_batches /= batch_size
> 
>     # allocate symbolic variables for the data
>     index = T.lscalar()  # index to a [mini]batch
> 
>     # start-snippet-1
>     x = T.matrix('x')   # the data is presented as rasterized images
>     y = T.ivector('y')  # the labels are presented as 1D vector of
>                         # [int] labels
> 
>     ######################
>     # BUILD ACTUAL MODEL #
>     ######################
>     print '... building the model'
> 
>     # Reshape matrix of rasterized images of shape (batch_size, 28 * 28)
>     # to a 4D tensor, compatible with our LeNetConvPoolLayer
>     # (28, 28) is the size of MNIST images.
>     layer0_input = x.reshape((batch_size, 3, 104, 52))
> 
>     # Construct the first convolutional pooling layer:
>     # filtering reduces the image size to (28-5+1 , 28-5+1) = (24, 24)
>     # maxpooling reduces this further to (24/2, 24/2) = (12, 12)
>     # 4D output tensor is thus of shape(batch_size, nkerns[0], 12, 12)
>     layer0 = LeNetConvPoolLayer(
>         rng,
>         input=layer0_input,
>         image_shape=(batch_size, 3, 104, 52),
>         filter_shape=(nkerns[0], 3, 5, 5),
>         poolsize=(2, 2)
>     )
> 
>     salidas_capa0 = theano.function(
>     [index],
>     layer0.salidas_capa,
>     givens={
>     x: test_set_x[index * batch_size: (index + 1) * batch_size],
>     }
>     )
> 
> 
>     # Construct the second convolutional pooling layer
>     # filtering reduces the image size to (12-5+1, 12-5+1) = (8, 8)
>     # maxpooling reduces this further to (8/2, 8/2) = (4, 4)
>     # 4D output tensor is thus of shape (batch_size, nkerns[1], 4, 4)
>     layer1 = LeNetConvPoolLayer(
>         rng,
>         input=layer0.output,
>         image_shape=(batch_size, nkerns[0], 50, 24),
>         filter_shape=(nkerns[1], nkerns[0], 5, 5),
>         poolsize=(2, 2)
>     )
> 
>     salidas_capa1 = theano.function(
>     [index],
>     layer1.salidas_capa,
>     givens={
>     x: test_set_x[index * batch_size: (index + 1) * batch_size],
>     }
>     )
> 
>     # the HiddenLayer being fully-connected, it operates on 2D matrices of
>     # shape (batch_size, num_pixels) (i.e matrix of rasterized images).
>     # This will generate a matrix of shape (batch_size, nkerns[1] * 4 * 4),
>     # or (500, 50 * 4 * 4) = (500, 800) with the default values.
>     layer2_input = layer1.output.flatten(2)
> 
>     # construct a fully-connected sigmoidal layer
>     layer2 = HiddenLayer(
>         rng,
>         input=layer2_input,
>         n_in=nkerns[1] * 23 * 10,
>         n_out=100,
>         activation=T.nnet.relu
>     )
> 
>     # classify the values of the fully-connected sigmoidal layer
>     svm = SVC()
> 
> 
>     layer3 = LogisticRegression(input=layer2.output, n_in=100, n_out=4)
> 
> 
>     salidas_capa3 = theano.function(
>         [index],
>         layer3.y_pred,
>         givens={
>             x: test_set_x[index * batch_size: (index + 1) * batch_size]
>         }
>     )
> 
>     # the cost we minimize during training is the NLL of the model
>     cost = layer3.negative_log_likelihood(y)
> 
>     # create a function to compute the mistakes that are made by the model
>     test_model = theano.function(
>         [index],
>         layer3.errors(y),
>         givens={
>             x: test_set_x[index * batch_size: (index + 1) * batch_size],      
> #Los given son algo para la gpu
>             y: test_set_y[index * batch_size: (index + 1) * batch_size]
>         }
>     )
> 
> 
>     validate_model = theano.function(
>         [index],
>         layer3.errors(y),
>         givens={
>             x: valid_set_x[index * batch_size: (index + 1) * batch_size],
>             y: valid_set_y[index * batch_size: (index + 1) * batch_size]
>         }
>     )
> 
>     # create a list of all model parameters to be fit by gradient descent
>     params = layer3.params + layer2.params + layer1.params + layer0.params
> 
>     # create a list of gradients for all model parameters
>     grads = T.grad(cost, params)
> 
>     # train_model is a function that updates the model parameters by
>     # SGD Since this model has many parameters, it would be tedious to
>     # manually create an update rule for each model parameter. We thus
>     # create the updates list by automatically looping over all
>     # (params[i], grads[i]) pairs.
>     updates = [
>         (param_i, param_i - learning_rate * grad_i)
>         for param_i, grad_i in zip(params, grads)
>     ]
> 
>     train_model = theano.function(
>         [index],
>         cost,
>         updates=updates,
>         givens={
>             x: train_set_x[index * batch_size: (index + 1) * batch_size],
>             y: train_set_y[index * batch_size: (index + 1) * batch_size]
>         }
>     )
>     # end-snippet-1
> 
> 
>     ###############
>     # TRAIN MODEL #
>     ###############
>     print '... training'
>     # early-stopping parameters
>     patience = 10000  # look as this many examples regardless
>     patience_increase = 2  # wait this much longer when a new best is found
>     improvement_threshold = 0.995  # a relative improvement of this much is
>     # considered significant
>     validation_frequency = min(n_train_batches, patience / 2)
>     # go through this many minibatche before checking the network
>     # on the validation set; in this case we check every epoch
> 
>     best_validation_loss = numpy.inf
>     best_iter = 0
>     test_score = 0.
>     start_time = timeit.default_timer()
> 
>     lista_coste = []
> 
>     epoch = 0
>     done_looping = False
> 
>     # save_file = 
> open('/home/beaa/PycharmProjects/TFM/LearningTheano/salidas0.pkl', 'wb')
>     # save_file1 = 
> open('/home/beaa/PycharmProjects/TFM/LearningTheano/wb0.pkl', 'wb')
>     # save_file2 = 
> open('/home/beaa/PycharmProjects/TFM/LearningTheano/salidas1.pkl', 'wb')
>     # save_file3 = 
> open('/home/beaa/PycharmProjects/TFM/LearningTheano/wb1.pkl', 'wb')
> 
>     print 'n_train_batches'
>     print n_train_batches
>     while (epoch < n_epochs) and (not done_looping):
>         epoch = epoch + 1
>         for minibatch_index in range(n_train_batches):
> 
>             iter = (epoch - 1) * n_train_batches + minibatch_index
> 
>             if iter % 100 == 0:
>                 print('training @ iter = ', iter)
>             cost_ij = train_model(minibatch_index)
> 
>             if (iter + 1) % validation_frequency == 0:
> 
>                 # compute zero-one loss on validation set
> 
>                 validation_losses = [validate_model(i) for i
>                                      in range(n_valid_batches)]
>                 this_validation_loss = numpy.mean(validation_losses)
>                 print('epoch %i, minibatch %i/%i, validation error %f %%' %
>                       (epoch, minibatch_index + 1, n_train_batches,
>                        this_validation_loss * 100.))
> 
>                 # if we got the best validation score until now
>                 if this_validation_loss < best_validation_loss:
> 
>                     # improve patience if loss improvement is good enough
>                     if this_validation_loss < best_validation_loss * \
>                             improvement_threshold:
>                         patience = max(patience, iter * patience_increase)
> 
>                     # save best validation score and iteration number
>                     best_validation_loss = this_validation_loss
>                     best_iter = iter
> 
>                     w0_test = layer0.W.get_value()
>                     b0_test = layer0.b.get_value()
> 
>                     w1_test = layer1.W.get_value()
>                     b1_test = layer1.b.get_value()
> 
>                     w2_test = layer2.W.get_value()
>                     b2_test = layer2.b.get_value()
> 
> 
> 
> 
>             if patience <= iter:
>                 done_looping = True
>                 break
> 
> 
>     ###############################
>     ###    TESTING MODEL        ###
>     ###############################
>     #Aqui se tiene que cargar la red
> 
>     layer0.W.set_value(w0_test)
>     layer0.b.set_value(b0_test)
> 
>     layer1.W.set_value(w1_test)
>     layer1.b.set_value(b1_test)
> 
>     layer2.W.set_value(w2_test)
>     layer2.b.set_value(b2_test)
> 
>     # test it on the test set
>     for i in range(n_test_batches):
>         test_losses = [test_model(i)]
>         y_pred_test = salidas_capa3[test_model(i)]
>         print y_pred_test
>         test_score = numpy.mean(test_losses)
> 
>     print((' test error of best model %f %%') % (test_score * 100.))
> 
> 
>     end_time = timeit.default_timer()
>     print('Optimization complete.')
>     print('Best validation score of %f %% obtained at iteration %i, '
>           'with test performance %f %%' %
>           (best_validation_loss * 100., best_iter + 1, test_score * 100.))
> 
>     print("start time: %d" % start_time)
>     print("end_time: %d" % end_time)
> 
>     # print(('The code for file ' +
>     #        os.path.split(__file__)[1] +
>     #        ' ran for %.2fm' % ((end_time - start_time) / 60.)), 
> file=sys.stderr)
> 
> 
> 
> 
> if __name__ == '__main__':
>     evaluate_lenet5()
> 
> 
> def experiment(state, channel):
>     evaluate_lenet5(state.learning_rate, dataset=state.dataset)
> 
> 
> 
> 
> 
> 
> Thanks in advance.
> 
> 
> Regards.
> 
> 
> 
> 
> 
> 
> 
> El martes, 28 de mayo de 2013, 23:56:29 (UTC+2), Pascal Lamblin escribió:
> >
> > On Wed, May 29, 2013, Анатолий wrote: 
> > > That's how i define test values: 
> > > 
> > > ishape = (288, 384) 
> > > batch_size = numpy.int16(batch_size) 
> > > index = T.lscalar().tag.test_value = numpy.int16(0) 
> > >  x = T.matrix('x').tag.test_value = numpy.random.rand(batch_size, 
> > > numpy.prod(ishape)).astype(dtype=theano.config.floatX) 
> > >  y = T.ivector('y').tag.test_value = numpy.random.randint(0,2, 
> > > size=batch_size).astype(dtype=numpy.int16) 
> >
> > In fact, as Olivier Delalleau pointed it, here is your problem. Here, 
> > you assign the test value to the name 'x', rather than assigning the 
> > symbolic variable. You should do the following instead: 
> >
> > # In two different statements: 
> > # first create 'index' as a symbolic variable 
> > index = T.lscalar() 
> > # then assign a test value to it 
> > index.tag.test_value = numpy.int16(0) 
> >
> > # same thing for the other variables: 
> > x = T.matrix('x') 
> > x.tag.test_value = numpy.random.rand(batch_size, 
> > numpy.prod(ishape)).astype(dtype=theano.config.floatX) 
> >
> > y = T.ivector('y') 
> > y.tag.test_value = numpy.random.randint(0, 2, 
> > size=batch_size).astype(dtype=numpy.int16) 
> >
> > Hope this helps, 
> > -- 
> > Pascal 
> >
> 
> -- 
> 
> --- 
> You received this message because you are subscribed to the Google Groups 
> "theano-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.


-- 
Pascal

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to