Пример #1
0
def test_classification():
    # Make dataset
    n_classes = 2
    n_samples = 1000
    n_features = 48
    x, y = make_classification(n_samples=n_samples,
                               n_features=n_features,
                               n_classes=n_classes,
                               n_informative=n_classes * 2,
                               random_state=1)
    x = x.astype(dp.float_)
    y = y.astype(dp.int_)
    n_train = int(0.8 * n_samples)
    x_train = x[:n_train]
    y_train = y[:n_train]
    x_test = x[n_train:]
    y_test = y[n_train:]

    scaler = dp.StandardScaler()
    x_train = scaler.fit_transform(x_train)
    x_test = scaler.transform(x_test)

    # Setup input
    batch_size = 16
    train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size)
    test_input = dp.Input(x_test)

    # Setup neural network
    weight_decay = 1e-03
    net = dp.NeuralNetwork(
        layers=[
            dp.Affine(
                n_out=32,
                weights=dp.Parameter(dp.AutoFiller(),
                                     weight_decay=weight_decay),
            ),
            dp.ReLU(),
            dp.Affine(
                n_out=64,
                weights=dp.Parameter(dp.AutoFiller(),
                                     weight_decay=weight_decay),
            ),
            dp.ReLU(),
            dp.Affine(
                n_out=n_classes,
                weights=dp.Parameter(dp.AutoFiller()),
            ),
        ],
        loss=dp.SoftmaxCrossEntropy(),
    )

    # Train neural network
    learn_rule = dp.Momentum(learn_rate=0.01 / batch_size, momentum=0.9)
    trainer = dp.GradientDescent(net, train_input, learn_rule)
    trainer.train_epochs(n_epochs=10)

    # Evaluate on test data
    error = np.mean(net.predict(test_input) != y_test)
    print('Test error rate: %.4f' % error)
    assert error < 0.2
Пример #2
0
    def train_minibatch(self, minibatch):
        """
        Train function that transforms (state,action,reward,state) into (input, expected_output) for neural net
        and trains the network
        @param minibatch: list of arrays: prestates, actions, rewards, poststates
        """
        prestates, actions, rewards, poststates = minibatch
        prestates = dp.Input(prestates)

        # predict Q-values for prestates, so we can keep Q-values for other actions unchanged
        qvalues = self.nnet.predict(prestates)

        # predict Q-values for poststates
        post_qvalues = self.nnet.predict(poststates)

        # take maximum Q-value of all actions
        max_qvalues = np.max(post_qvalues, axis=1)

        # update the Q-values for the actions we actually performed
        # remember delta value for prioritized sweeping
        for i, action in enumerate(actions):
            qvalues[i][
                action] = rewards[i] + self.discount_factor * max_qvalues[i]

        train_input = dp.SupervisedInput(prestates.x,
                                         qvalues,
                                         batch_size=self.minibatch_size)

        self.trainer.train(net, train_input)
Пример #3
0
dataset = dp.dataset.MNIST()
x_train, y_train, x_test, y_test = dataset.data(dp_dtypes=True)

# Bring images to BCHW format
x_train = x_train[:, np.newaxis, :, :]
x_test = x_test[:, np.newaxis, :, :]

# Normalize pixel intensities
scaler = dp.StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)

# Prepare network inputs
batch_size = 128
train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size)
test_input = dp.Input(x_test)


# Setup network
def pool_layer():
    return dp.Pool(
        win_shape=(2, 2),
        strides=(2, 2),
        border_mode='valid',
        method='max',
    )


def conv_layer(n_filters):
    return dp.Convolution(
        n_filters=n_filters,
Пример #4
0
        y_train = y_train.astype('int')

        # Normalize pixel intensities
        scaler = dp.UniformScaler()
        x_train = scaler.fit_transform(x_train)
        x_test = scaler.transform(x_test)

        # Shufflinig
        ind = range(len(y_train))
        np.random.shuffle(ind)
        y_train = y_train[ind]
        x_train = x_train[ind, :]

        # Prepare autoencoder input
        batch_size = 50
        train_input = dp.Input(x_train, batch_size=batch_size)

        # Setup autoencoders
        sae = dp.StackedAutoencoder(layers=[
            dp.DenoisingAutoencoder(
                n_out=300,
                weights=dp.Parameter(dp.AutoFiller()),
                activation='sigmoid',
                corruption=0.1,
            ),
            dp.DenoisingAutoencoder(
                n_out=100,
                weights=dp.Parameter(dp.AutoFiller()),
                activation='sigmoid',
                corruption=0.1,
            ),
Пример #5
0
    def play_games(self, nr_frames, epoch, train, epsilon=None):
        """
        Main cycle: starts a game and plays number of frames.
        @param nr_frames: total number of games allowed to play
        @param train: true or false, whether to do training or not
        @param epsilon: fixed epsilon, only used when not training
        """

        frames_played = 0
        game_scores = []

        first_frame = self.ale.new_game()
        if train: self.memory.add_first(first_frame)

        if self.current_state == None:
            self.current_state = np.empty((1, self.state_length, 84, 84),
                                          dtype=np.float64)
            for i in range(self.state_length):
                self.current_state[0, i, :, :] = first_frame.copy()
        else:
            self.current_state.x[0, :-1, :, :] = self.current_state.x[0,
                                                                      1:, :, :]
            self.current_state.x[0, -1, :, :] = first_frame.copy()

        game_score = 0

        if train and epoch == 1:
            self.current_state = dp.Input(self.current_state)
            self.current_state.y_shape = (1, self.number_of_actions)
            self.nnet._setup(self.current_state)

        while frames_played < nr_frames:
            if train:
                epsilon = self.compute_epsilon(self.total_frames_trained)

            if random.uniform(0, 1) < epsilon:
                action = random.choice(range(self.number_of_actions))
            else:
                action = self.predict_action(self.current_state, train)

            points, next_frame = self.ale.move(action)

            # Changing points to rewards
            if points > 0:
                print "    Got %d points" % points
                reward = 1
            elif points < 0:
                print "    Lost %d points" % points
                reward = -1
            else:
                reward = 0

            game_score += points
            frames_played += 1

            self.current_state.x[0, :-1, :, :] = self.current_state.x[0,
                                                                      1:, :, :]
            self.current_state.x[0, -1, :, :] = next_frame

            if train:
                self.memory.add(action, reward, next_frame)
                self.total_frames_trained += 1
                minibatch = self.memory.get_minibatch(self.minibatch_size)
                self.train_minibatch(minibatch)

            if self.ale.game_over:
                print "    Game over, score = %d" % game_score
                # After "game over" increase the number of games played
                game_scores.append(game_score)
                game_score = 0

                # And do stuff after end game
                self.ale.end_game()
                if train: self.memory.add_last()

                first_frame = self.ale.new_game()
                if train: self.memory.add_first(first_frame)

                self.current_state.x[0, :-1, :, :] = self.current_state.x[
                    0, 1:, :, :]
                self.current_state.x[0, -1, :, :] = first_frame.copy()

        self.ale.end_game()

        return game_scores
Пример #6
0

# Fetch CIFAR10 data
dataset = dp.dataset.CIFAR10()
x_train, y_train, x_test, y_test = dataset.data(dp_dtypes=True)

# Normalize pixel intensities
scaler = dp.StandardScaler()
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)

# Prepare network inputs
batch_size = 128
train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size)
test_input = dp.Input(x_test, batch_size=batch_size)

# Setup network
def conv_layer(n_filters):
    return dp.Convolution(
        n_filters=32,
        filter_shape=(5, 5),
        border_mode='full',
        weights=dp.Parameter(dp.AutoFiller(gain=1.25), weight_decay=0.003),
    )

def pool_layer():
    return dp.Pool(
        win_shape=(3, 3),
        strides=(2, 2),
        border_mode='same',
Пример #7
0
def test_classification():
    # Make dataset
    n_classes = 2
    n_samples = 1000
    n_features = 48
    x, y = make_classification(
        n_samples=n_samples, n_features=n_features, n_classes=n_classes,
        n_informative=n_classes*2, random_state=1
    )

    n_train = int(0.8 * n_samples)
    n_val = int(0.5 * (n_samples - n_train))

    x_train = x[:n_train]
    y_train = y[:n_train]
    x_val = x[n_train:n_train+n_val]
    y_val = y[n_train:n_train+n_val]
    x_test = x[n_train+n_val:]
    y_test = y[n_train+n_val:]

    scaler = dp.StandardScaler()
    x_train = scaler.fit_transform(x_train)
    x_val = scaler.transform(x_val)
    x_test = scaler.transform(x_test)

    # Setup input
    batch_size = 16
    train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size)
    val_input = dp.Input(x_val)
    test_input = dp.Input(x_test)

    # Setup neural network
    weight_decay = 1e-03
    net = dp.NeuralNetwork(
        layers=[
            dp.FullyConnected(
                n_out=32,
                weights=dp.Parameter(dp.AutoFiller(),
                                     weight_decay=weight_decay),
            ),
            dp.Activation('relu'),
            dp.FullyConnected(
                n_out=64,
                weights=dp.Parameter(dp.AutoFiller(),
                                     weight_decay=weight_decay),
            ),
            dp.Activation('relu'),
            dp.FullyConnected(
                n_out=n_classes,
                weights=dp.Parameter(dp.AutoFiller()),
            ),
        ],
        loss=dp.SoftmaxCrossEntropy(),
    )

    # Train neural network
    def val_error():
        return np.mean(net.predict(val_input) != y_val)
    trainer = dp.StochasticGradientDescent(
        min_epochs=10, learn_rule=dp.Momentum(learn_rate=0.01, momentum=0.9),
    )
    trainer.train(net, train_input, val_error)

    # Evaluate on test data
    error = np.mean(net.predict(test_input) != y_test)
    print('Test error rate: %.4f' % error)
    assert error < 0.2