Пример #1
0
    def test_stacked_lstm(self):
        x_train, x_test, y_train, y_test = self.data
        network = algorithms.RMSProp(
            [
                layers.Input(self.n_time_steps),
                layers.Embedding(self.n_categories, 10),
                layers.LSTM(
                    n_units=10,
                    only_return_final=False,
                    input_weights=init.Normal(0.1),
                    hidden_weights=init.Normal(0.1),
                ),
                layers.LSTM(
                    n_units=2,
                    input_weights=init.Normal(0.1),
                    hidden_weights=init.Normal(0.1),
                ),
                layers.Sigmoid(1),
            ],
            step=0.05,
            verbose=False,
            batch_size=1,
            loss='binary_crossentropy',
        )
        network.train(x_train, y_train, x_test, y_test, epochs=20)

        y_predicted = network.predict(x_test).round()
        accuracy = (y_predicted.T == y_test).mean()

        self.assertGreaterEqual(accuracy, 0.8)
Пример #2
0
 def test_simple_rmsprop(self):
     x_train, x_test, y_train, y_test = simple_classification()
     mnet = algorithms.RMSProp(
         (10, 20, 1),
         step=0.02,
         batch_size='full',
         verbose=False,
         epsilon=1e-5,
         decay=0.9,
     )
     mnet.train(x_train, y_train, x_test, y_test, epochs=100)
     self.assertGreater(0.11, mnet.validation_errors.last())
Пример #3
0
 def test_simple_rmsprop(self):
     x_train, _, y_train, _ = simple_classification()
     mnet = algorithms.RMSProp(
         (10, 20, 1),
         step=.1,
         batch_size='full',
         verbose=False,
         epsilon=1e-5,
         decay=0.9,
     )
     mnet.train(x_train, y_train, epochs=100)
     self.assertAlmostEqual(0.01, mnet.errors.last(), places=2)
Пример #4
0
 def test_rmsprop(self):
     x_train, x_test, y_train, y_test = simple_classification()
     optimizer = algorithms.RMSProp(
         self.network,
         step=0.02,
         batch_size=None,
         verbose=False,
         epsilon=1e-5,
         decay=0.9,
     )
     optimizer.train(x_train, y_train, x_test, y_test, epochs=150)
     self.assertGreater(0.15, optimizer.errors.valid[-1])
Пример #5
0
    def train_lstm(self, data, **lstm_options):
        x_train, x_test, y_train, y_test = data
        network = algorithms.RMSProp(
            [
                layers.Input(self.n_time_steps),
                layers.Embedding(self.n_categories, 10),
                layers.LSTM(20, **lstm_options),
                layers.Sigmoid(1),
            ],
            step=0.05,
            verbose=False,
            batch_size=16,
            error='binary_crossentropy',
        )
        network.train(x_train, y_train, x_test, y_test, epochs=20)

        y_predicted = network.predict(x_test).round()
        accuracy = (y_predicted.T == y_test).mean()
        return accuracy
Пример #6
0
	def select_algorithm(self, algorithm, options=None):
		try:
			self.network = algorithms.LevenbergMarquardt(self.layers)
			opt = options
			print(opt[1])
			print("Wybrano optymalizator: " + str(algorithm))
		except RecursionError:
			print("Problem rekursji")
			return None

		if algorithm == 'GradientDescent':
			self.network = algorithms.GradientDescent(self.layers)
		if algorithm == 'LevenbergMarquardt':
			self.network = algorithms.LevenbergMarquardt(connection=self.layers, mu=opt[0], mu_update_factor=opt[1])
		if algorithm == 'Adam':
			self.network = algorithms.Adam(self.layers)
		if algorithm == 'QuasiNewton':
			self.network = algorithms.QuasiNewton(self.layers)
		if algorithm == 'Quickprop':
			self.network = algorithms.Quickprop(self.layers)
		if algorithm == 'MinibatchGradientDescent':
			self.network = algorithms.MinibatchGradientDescent(self.layers)
		if algorithm == 'ConjugateGradient':
			self.network = algorithms.ConjugateGradient(self.layers)
		if algorithm == 'Hessian':
			self.network = algorithms.Hessian(self.layers)
		if algorithm == 'HessianDiagonal':
			self.network = algorithms.HessianDiagonal(self.layers)
		if algorithm == 'Momentum':
			self.network = algorithms.Momentum(self.layers)
		if algorithm == 'RPROP':
			self.network = algorithms.RPROP(self.layers)
		if algorithm == 'IRPROPPlus':
			self.network = algorithms.IRPROPPlus(self.layers)
		if algorithm == 'Adadelta':
			self.network = algorithms.Adadelta(self.layers)
		if algorithm == 'Adagrad':
			self.network = algorithms.Adagrad(self.layers)
		if algorithm == 'RMSProp':
			self.network = algorithms.RMSProp(self.layers)
		if algorithm == 'Adamax':
			self.network = algorithms.Adamax(self.layers)
Пример #7
0
    def test_stacked_gru(self):
        x_train, x_test, y_train, y_test = self.data
        network = algorithms.RMSProp(
            [
                layers.Input(self.n_time_steps),
                layers.Embedding(self.n_categories, 10),
                layers.GRU(10, only_return_final=False),
                layers.GRU(1),
                layers.Sigmoid(1),
            ],
            step=0.01,
            verbose=False,
            batch_size=1,
            loss='binary_crossentropy',
        )
        network.train(x_train, y_train, x_test, y_test, epochs=10)

        y_predicted = network.predict(x_test).round()
        accuracy = (y_predicted.T == y_test).mean()

        self.assertGreaterEqual(accuracy, 0.8)
Пример #8
0
    def test_gru_with_4d_input(self):
        x_train, x_test, y_train, y_test = self.data
        network = algorithms.RMSProp(
            [
                layers.Input(self.n_time_steps),
                layers.Embedding(self.n_categories, 10),
                # Make 4D input
                layers.Reshape((self.n_time_steps, 5, 2), name='reshape'),
                layers.GRU(10),
                layers.Sigmoid(1),
            ],
            step=0.1,
            verbose=False,
            batch_size=1,
            error='binary_crossentropy',
        )
        network.train(x_train, y_train, x_test, y_test, epochs=2)

        reshape = network.connection.end('reshape')
        # +1 for batch size
        output_dimension = len(reshape.output_shape) + 1
        self.assertEqual(4, output_dimension)
Пример #9
0
def train_network(parameters):
    print("Parameters:")
    pprint(parameters)
    print()

    step = parameters['step']
    batch_size = int(parameters['batch_size'])
    proba = parameters['dropout']
    activation_layer = parameters['act_func_type']
    layer_sizes = [int(n) for n in parameters['layers']['n_units_layer']]

    network = layers.Input(784)

    for layer_size in layer_sizes:
        network = network > activation_layer(layer_size)

    network = network > layers.Dropout(proba) > layers.Softmax(10)

    mnet = algorithms.RMSProp(
        network,
        batch_size=batch_size,
        step=step,
        error='categorical_crossentropy',
        shuffle_data=True,
        epoch_end_signal=on_epoch_end,
    )
    mnet.train(x_train, y_train, epochs=50)

    score = mnet.prediction_error(x_test, y_test)

    y_predicted = mnet.predict(x_test).argmax(axis=1)
    accuracy = metrics.accuracy_score(y_test.argmax(axis=1), y_predicted)

    print("Final score: {}".format(score))
    print("Accuracy: {:.2%}".format(accuracy))

    return score
def on_epoch_end(network):
    if network.errors.last() > 10:
        raise StopTraining("Training was interrupted. Error is to high.")

        mnet = algorithms.RMSProp(
            network,
            batch_size=batch_size,
            step=step,
            error='categorical_crossentropy',
            shuffle_data=True,
            epoch_end_signal=on_epoch_end,
        )

        mnet.train(x_train, y_train, epochs=50)

        score = mnet.prediction_error(x_test, y_test)

        y_predicted = mnet.predict(x_test).argmax(axis=1)
        accuracy = metrics.accuracy_score(y_test.argmax(axis=1), y_predicted)

        print("Final score: {}".format(score))
        print("Accuracy: {:.2%}".format(accuracy))

        return score
Пример #11
0
    def test_stacked_gru_with_enabled_backwards_option(self):
        x_train, x_test, y_train, y_test = self.data
        x_train = x_train[:, ::-1]
        x_test = x_test[:, ::-1]

        network = algorithms.RMSProp(
            [
                layers.Input(self.n_time_steps),
                layers.Embedding(self.n_categories, 10),
                layers.GRU(10, only_return_final=False, backwards=True),
                layers.GRU(2, backwards=True),
                layers.Sigmoid(1),
            ],
            step=0.1,
            verbose=False,
            batch_size=1,
            error='binary_crossentropy',
        )
        network.train(x_train, y_train, x_test, y_test, epochs=20)

        y_predicted = network.predict(x_test).round()
        accuracy = (y_predicted.T == y_test).mean()

        self.assertGreaterEqual(accuracy, 0.9)
Пример #12
0
x_train, x_test, y_train, y_test = train_test_split(data,
                                                    labels,
                                                    train_size=0.8)

n_categories = len(reber.avaliable_letters) + 1  # +1 for zero paddings
n_time_steps = x_train.shape[1]

network = algorithms.RMSProp(
    [
        layers.Input(n_time_steps),
        # shape: (n_samples, n_time_steps)
        layers.Embedding(n_categories, 10),
        # shape: (n_samples, n_time_steps, 10)

        # unroll_scan - speed up calculation for short sequences
        layers.GRU(20, unroll_scan=True),
        # shape: (n_samples, 20)
        layers.Sigmoid(1),
        # shape: (n_samples, 1)
    ],
    step=0.05,
    verbose=True,
    batch_size=64,
    error='binary_crossentropy',
)
network.train(x_train, y_train, x_test, y_test, epochs=20)

y_predicted = network.predict(x_test).round()
accuracy = (y_predicted.T == y_test).mean()
print("Test accuracy: {:.2%}".format(accuracy))
Пример #13
0
if __name__ == '__main__':
    window_size = 40

    print("Loading Shakespeare's text ...")
    preprocessor = TextPreprocessing(filepath=TEXT_FILE)
    n_characters = preprocessor.n_characters

    x_train, x_test, y_train, y_test = preprocessor.load_samples(
        window_size, stride=10)

    network = algorithms.RMSProp(
        [
            layers.Input((window_size, n_characters)),
            layers.LSTM(128),
            layers.Softmax(n_characters),
        ],
        step=0.01,
        verbose=True,
        batch_size=128,
        loss='categorical_crossentropy',
    )
    network.train(x_train, y_train, x_test, y_test, epochs=10)

    # Number of symbols that will be generated
    n_new_symbols = 1000
    # Which samples to use from the test data
    test_sample_id = 0

    test_sample = x_test[test_sample_id]
    int_sequence = list(test_sample.argmax(axis=1))
Пример #14
0
    x_train, s1_train, s2_train, y_train = load_data(env['train_data_file'])
    x_test, s1_test, s2_test, y_test = load_data(env['test_data_file'])

    print("Initializing VIN...")
    network = algorithms.RMSProp(
        create_VIN(
            env['input_image_shape'],
            n_hidden_filters=150,
            n_state_filters=10,
            k=env['k'],
        ),
        verbose=True,

        # Loss function applies categorical cross entropy
        # in a bit more efficient way.
        loss=loss_function,

        # Shape of the target value might be different compare to the
        # expected shape of the output. Without this change network will
        # assume that target shape will be the same as network's output
        # shape, which is (None, 8)
        target=tf.placeholder(tf.float32, shape=(None, None)),

        # Signal will ensure that step (learning rate) will be reduced
        # after certain number of iterations
        signals=on_epoch_end_from_steps(env['steps']),
        **env['training_options'])

    print("Training VIN...")
    network.train(
        (x_train, s1_train, s2_train),
Пример #15
0
    args = parser.parse_args()
    env = environments[args.imsize]

    x_train, s1_train, s2_train, y_train = load_data(env['train_data_file'])
    x_test, s1_test, s2_test, y_test = load_data(env['test_data_file'])

    network = algorithms.RMSProp(
        create_VIN(
            env['input_image_shape'],
            n_hidden_filters=150,
            n_state_filters=10,
            k=env['k'],
        ),

        step=0.01,
        verbose=True,
        batch_size=12,
        error=loss_function,
        epoch_end_signal=on_epoch_end,

        decay=0.9,
        epsilon=1e-6,
    )
    network.train((x_train, s1_train, s2_train), y_train,
                  (x_test, s1_test, s2_test), y_test,
                  epochs=120)

    if not os.path.exists(MODELS_DIR):
        os.mkdir(MODELS_DIR)
Пример #16
0
        '--pretrained',
        dest='use_pretrained',
        action='store_true',
        help='load pretrained network from file and play without training')
    args = parser.parse_args()

    network = algorithms.RMSProp(
        [
            layers.Input(4),
            layers.Relu(64),
            layers.Relu(48),
            layers.Relu(32),
            layers.Relu(64) > layers.Dropout(0.2),

            # Expecting two different actions:
            # 1. Move left
            # 2. Move right
            layers.Linear(2),
        ],
        step=0.001,
        error='rmse',
        batch_size=100,
        decay_rate=0.1,
        addons=[algorithms.WeightDecay],
    )

    env = gym.make('CartPole-v0')
    env.seed(0)  # To make results reproducible for the gym

    memory_size = 1000  # Number of samples stored in the memory
    memory = deque(maxlen=memory_size)
Пример #17
0
                    ],
                    [
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1,
                        0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                        1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
                        1, 0, 0, 0, 0, 0, 0, 0, 0, 0
                    ],
                    [
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0,
                        0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
                        0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1,
                        1, 0, 0, 0, 0, 0, 0, 0, 0, 0
                    ]])

y_train = np.array([1, 0, 2, 3])
normalizer = max(y_train)
y_train_normalized = y_train / normalizer
network = algorithms.RMSProp(
    [
        layers.Input(64),
        layers.Sigmoid(2),
        layers.Sigmoid(1),
    ],
    step=0.1,
)

network.train(x_train, y_train_normalized, epochs=10000)

with open('network.pickle', 'wb') as f:
    pickle.dump(network, f)
Пример #18
0
# Construct Variational Autoencoder
encoder = layers.Input(784) > layers.Tanh(500)

mu = layers.Linear(2, name='mu')
sigma = layers.Linear(2, name='sigma')
sampler = [mu, sigma] > GaussianSample()

decoder = layers.Tanh(500) > layers.Sigmoid(784)

# Train network
network = algorithms.RMSProp(
    encoder > sampler > decoder,

    error=vae_loss,
    batch_size=128,
    shuffle_data=True,
    step=0.001,
    verbose=True,

    decay_rate=0.01,
    addons=[algorithms.WeightDecay],
)

x_train, x_test = load_data()
network.train(x_train, x_train, x_test, x_test, epochs=50)

# Sample digits from the obtained distribution
generator = algorithms.GradientDescent(layers.Input(2) > decoder)
generate_and_plot_sampels(generator, 20, 20)
Пример #19
0
            Linear(2, name='mu') >> Collect('mu'),
            Linear(2, name='sigma') >> Collect('sigma'),
        ),
        GaussianSample(),

        # Decoder
        # Note: Identity layer acts as a reference. Using it
        # we can easily cut the decoder from the network
        Identity('decoder'),
        Tanh(256),
        Sigmoid(784),
    )

    # Train network
    optimizer = algorithms.RMSProp(
        network,
        loss=vae_loss,
        regularizer=algorithms.l2(0.001),
        batch_size=128,
        shuffle_data=True,
        step=0.001,
        verbose=True,
    )

    x_train, x_test = load_data()
    optimizer.train(x_train, x_train, x_test, x_test, epochs=50)

    # Sample digits from the obtained distribution
    generator = Input(2) >> network.start('decoder')
    generate_and_plot_sampels(generator, 15, 15)
Пример #20
0
if __name__ == '__main__':
    args = parser.parse_args()
    env = environments[args.imsize]

    print("Loading train and test data...")
    x_train, s1_train, s2_train, y_train = load_data(env['train_data_file'])
    x_test, s1_test, s2_test, y_test = load_data(env['test_data_file'])

    print("Initializing VIN...")
    network = algorithms.RMSProp(
        create_VIN(
            env['input_image_shape'],
            n_hidden_filters=150,
            n_state_filters=10,
            k=env['k'],
        ),

        verbose=True,
        error=loss_function,
        epoch_end_signal=on_epoch_end_from_steps(env['steps']),
        **env['training_options']
    )

    print("Training VIN...")
    network.train(
        (x_train, s1_train, s2_train), y_train,
        (x_test, s1_test, s2_test), y_test,
        epochs=env['epochs'],
    )

    if not os.path.exists(MODELS_DIR):
Пример #21
0
        '-p',
        '--pretrained',
        dest='use_pretrained',
        action='store_true',
        help='load pretrained network from file and play without training',
    )
    args = parser.parse_args()
    network = algorithms.RMSProp(
        [
            layers.Input(4),
            layers.Relu(64),
            layers.Relu(48),
            layers.Relu(32),
            layers.Relu(64) > layers.Dropout(0.2),

            # Expecting two different actions:
            # 1. Move left
            # 2. Move right
            layers.Linear(2),
        ],
        step=0.0005,
        error='rmse',
        batch_size='full',
        verbose=False)

    env = gym.make('CartPole-v0')
    env.seed(0)  # To make results reproducible for the gym

    memory_size = 1000  # Number of samples stored in the memory
    memory = deque(maxlen=memory_size)