Exemplo n.º 1
0
    def create_iter_functions(self,
                              dataset,
                              output_layer,
                              X_tensor_type=T.matrix):
        batch_index = T.iscalar('batch_index')
        X_batch = X_tensor_type('x')
        y_batch = T.ivector('y')

        batch_slice = slice(batch_index * self.batch_size,
                            (batch_index + 1) * self.batch_size)

        objective = Objective(output_layer,
                              loss_function=categorical_crossentropy)

        loss_train = objective.get_loss(X_batch, target=y_batch)
        loss_eval = objective.get_loss(X_batch,
                                       target=y_batch,
                                       deterministic=True)

        pred = T.argmax(lasagne.layers.get_output(output_layer,
                                                  X_batch,
                                                  deterministic=True),
                        axis=1)
        proba = lasagne.layers.get_output(output_layer,
                                          X_batch,
                                          deterministic=True)
        accuracy = T.mean(T.eq(pred, y_batch), dtype=theano.config.floatX)

        all_params = get_all_params(output_layer)
        updates = lasagne.updates.nesterov_momentum(loss_train, all_params,
                                                    self.lr, self.momentum)

        iter_train = theano.function(
            [batch_index],
            loss_train,
            updates=updates,
            givens={
                X_batch: dataset['X_train'][batch_slice],
                y_batch: dataset['y_train'][batch_slice],
            },
            on_unused_input='ignore',
        )

        iter_valid = None
        if self.use_valid:
            iter_valid = theano.function(
                [batch_index],
                [loss_eval, accuracy, proba],
                givens={
                    X_batch: dataset['X_valid'][batch_slice],
                    y_batch: dataset['y_valid'][batch_slice],
                },
            )

        return dict(train=iter_train, valid=iter_valid)
Exemplo n.º 2
0
    def test_objective_no_target(self):
        from lasagne.objectives import Objective

        input_layer = mock.Mock()
        loss_function = mock.Mock()
        input = object()
        objective = Objective(input_layer, loss_function)
        result = objective.get_loss(input)

        input_layer.get_output.assert_called_with(input)
        network_output = input_layer.get_output.return_value

        loss_function.assert_called_with(network_output, objective.target_var)
        assert result == loss_function.return_value.mean.return_value
Exemplo n.º 3
0
    def test_objective_no_target(self):
        from lasagne.objectives import Objective
        from lasagne.layers.input import Layer, InputLayer

        input_layer = mock.Mock(InputLayer((None, )), output_shape=(None, ))
        layer = mock.Mock(Layer(input_layer), output_shape=(None, ))
        layer.input_layer = input_layer
        loss_function = mock.Mock()
        input = theano.tensor.vector()
        objective = Objective(layer, loss_function)
        result = objective.get_loss(input)

        layer.get_output_for.assert_called_with(input)
        network_output = layer.get_output_for.return_value

        loss_function.assert_called_with(network_output, objective.target_var)
        assert result == loss_function.return_value.mean.return_value
Exemplo n.º 4
0
    def test_objective(self):
        from lasagne.objectives import Objective

        input_layer = mock.Mock()
        loss_function = mock.Mock()
        input, target, kwarg1 = object(), object(), object()
        objective = Objective(input_layer, loss_function)
        result = objective.get_loss(input, target, 'mean', kwarg1=kwarg1)

        # We expect that the input layer's `get_output` was called with
        # the `input` argument we provided, plus the extra positional and
        # keyword arguments.
        input_layer.get_output.assert_called_with(input, kwarg1=kwarg1)
        network_output = input_layer.get_output.return_value

        # The `network_output` and `target` are fed into the loss
        # function:
        loss_function.assert_called_with(network_output, target)
        assert result == loss_function.return_value.mean.return_value
Exemplo n.º 5
0
    def test_objective(self):
        from lasagne.objectives import Objective
        from lasagne.layers.input import Layer, InputLayer

        input_layer = mock.Mock(InputLayer((None, )), output_shape=(None, ))
        layer = mock.Mock(Layer(input_layer), output_shape=(None, ))
        layer.input_layer = input_layer
        loss_function = mock.Mock()
        input, target, kwarg1 = theano.tensor.vector(), object(), object()
        objective = Objective(layer, loss_function)
        result = objective.get_loss(input, target, 'mean', kwarg1=kwarg1)

        # We expect that the layer's `get_output_for` was called with
        # the `input` argument we provided, plus the extra positional and
        # keyword arguments.
        layer.get_output_for.assert_called_with(input, kwarg1=kwarg1)
        network_output = layer.get_output_for.return_value

        # The `network_output` and `target` are fed into the loss
        # function:
        loss_function.assert_called_with(network_output, target)
        assert result == loss_function.return_value.mean.return_value
Exemplo n.º 6
0
 def get_loss(self, loss_function, output, target, aggregation=None):
     from lasagne.objectives import Objective
     input_layer = self.input_layer(output)
     obj = Objective(input_layer, loss_function)
     return obj.get_loss(target=target, aggregation=aggregation)
Exemplo n.º 7
0
                        num_components=N_COMPONENTS,
                        min_sigma=0))

print("Total parameters: {}".format(
    sum([
        p.get_value().size for p in lasagne.layers.get_all_params(layers[-1])
    ])))

X = T.tensor3('X')
t = T.matrix('t')

# add test values
X.tag.test_value = floatX(np.random.rand(*SHAPE))
t.tag.test_value = floatX(np.random.rand(N_SEQ_PER_BATCH * SEQ_LENGTH, 1))

objective = Objective(layers[-1], loss_function=mdn_nll)
loss = objective.get_loss(X, t)

all_params = lasagne.layers.get_all_params(layers[-1])
updates = lasagne.updates.momentum(loss, all_params, learning_rate)

# Theano functions for training, getting output, and computing loss
print("Compiling Theano functions...")
train = theano.function([X, t], loss, updates=updates)
y_pred = theano.function([X], layers[-1].get_output(X))
compute_loss = theano.function([X, t], loss)
print("Done compiling Theano functions.")

# Train the net
costs = []
t_val = t_val.reshape((N_SEQ_PER_BATCH * SEQ_LENGTH, 1))