def compute_result(model, dataset, data): """Compute output value of data samples by using the trained model Args: model: A NeuralNet model dataset: A Dataset object returned by ``create_dataset`` data: list of pedestrian tuples returned by ``decomp_body`` Returns: A tuple (train, valid, test) where each is three matrices (image_tensor4D, output_matrix, target_matrix) """ result = cachem.load('result') if result is None: import theano import theano.tensor as T from reid.models.layers import CompLayer from reid.models.neural_net import NeuralNet model = NeuralNet([model, CompLayer()]) x = T.matrix() f = theano.function(inputs=[x], outputs=model.get_output(x)) def compute_output(X): outputs = [f(X[i:i+1, :]).ravel() for i in xrange(X.shape[0])] return numpy.asarray(outputs) images = numpy.asarray([p[0] for p in data]) train = (images[dataset.train_ind], compute_output(dataset.train_x.get_value(borrow=True)), dataset.train_y.get_value(borrow=True)) valid = (images[dataset.valid_ind], compute_output(dataset.valid_x.get_value(borrow=True)), dataset.valid_y.get_value(borrow=True)) test = (images[dataset.test_ind], compute_output(dataset.test_x.get_value(borrow=True)), dataset.test_y.get_value(borrow=True)) result = (train, valid, test) return result
multitask = DecompLayer([(1,), (3,)]) model = NeuralNet([decomp, columns, comp, multitask]) # Build up the target value adapter adapter = DecompLayer([(1,), (3,)]) # Build up evaluator evaluator = Evaluator(model, [mse, mse], [mse, mse], adapter) # Compute the expression by using the model x = T.matrix('x') y = T.matrix('y') output = model.get_output(x) cost, updates = evaluator.get_cost_updates(x, y, 1.0) error = evaluator.get_error(x, y) f = theano.function(inputs=[x, y], outputs=[cost, error]) cost, error = f(X, Y) print "The target matrix is ", Y print "The cost is ", cost print "The error is ", error cost, error = f(X, Z) print "The target matrix is ", Z print "The cost is ", cost print "The error is ", error
comp = CompLayer() multitask = DecompLayer([(1, ), (3, )]) model = NeuralNet([decomp, columns, comp, multitask]) # Build up the target value adapter adapter = DecompLayer([(1, ), (3, )]) # Build up evaluator evaluator = Evaluator(model, [mse, mse], [mse, mse], adapter) # Compute the expression by using the model x = T.matrix('x') y = T.matrix('y') output = model.get_output(x) cost, updates = evaluator.get_cost_updates(x, y, 1.0) error = evaluator.get_error(x, y) f = theano.function(inputs=[x, y], outputs=[cost, error]) cost, error = f(X, Y) print "The target matrix is ", Y print "The cost is ", cost print "The error is ", error cost, error = f(X, Z) print "The target matrix is ", Z print "The cost is ", cost print "The error is ", error