Exemple #1
0
    def __init__(self,
                 epsilon=0,
                 gamma=0.97,
                 memory_capacity=10000,
                 batch_size=4,
                 learning_rate=0.001,
                 Q_1=None,
                 num_inputs=None,
                 num_actions=None,
                 params_path=None,
                 memory_path=None):

        self.state = None
        self.is_terminal = False
        self.action = None
        self.reward = 0

        self.episode = 0

        # DQL
        self.epsilon = epsilon  # Exploitation or exploration
        self.gamma = gamma  # Discount factor
        self.memory_capacity = memory_capacity
        self.batch_size = batch_size
        self.learning_rate = learning_rate
        self.num_inputs = num_inputs
        self.num_actions = num_actions

        # Replay memory
        self.exp_bank = self.reset_memory()
        if memory_path and os.path.exists(memory_path):
            memory = np.load(memory_path, allow_pickle=True)
            self.exp_bank.memory = memory['exp_bank'].tolist()
            print("loaded memory_bank")

        # Action-value function
        if Q_1 is not None:
            self.Q_1 = Q_1
            self.num_inputs = self.Q_1.layers[0].input_shape
            self.num_actions = self.Q_1.layers[-1].output_shape
        elif num_actions is not None:
            self.Q_1 = models.Sequential()
            self.Q_1.add(
                layers.Dense(self.num_inputs, activation=activations.relu))
            self.Q_1.add(layers.Dense(10, activation=activations.sigmoid))
            self.Q_1.add(layers.Dense(20, activation=activations.sigmoid))
            self.Q_1.add(
                layers.Dense(self.num_actions, activation=activations.softmax))
            self.Q_1.compile()
        else:
            raise ValueError()

        if params_path:
            self.Q_1.load_params(params_path)
            self.Q_2 = self.Q_1

        self.Q_2 = self.Q_1
print('Loading data...')
dataset = load_mnist_data(file_path="400_imgs.npz")

M = models.Sequential()

M.add(layers.Reshape((1, 28, 28), input_shape=(1, 784)))
M.add(
    layers.Conv2D(15,
                  kernel_size=(3, 3),
                  input_shape=(1, 28, 28),
                  padding="same",
                  activation=activations.leaky_relu))
M.add(layers.MaxPooling2D((2, 2)))
M.add(layers.Flatten())
M.add(layers.Dense(100, activation=activations.leaky_relu))
M.add(layers.Dense(10, activation=activations.softmax))

M.compile()
M.description()

# params_path = "mnist_model.npz"
# if os.path.exists(params_path):
#     params = np.load(params_path, allow_pickle=True)
#     for key in params.files:
#         M.params[key] = params[key]

print("training ...")

my_callbacks = callbacks.Callbacks()
my_callbacks.on("train_example", train_example_callback)
Exemple #3
0
                     os.path.pardir)))

import numpy as np
from pysnaike import activations, layers, models
import matplotlib.pyplot as plt

# Create random dataset with 10000 training examples consisting of 10 random values each.
num_in = 10
options = 10
inputs = np.random.randint(0, options + 1, size=[10000, num_in])
# The output is the average value for each training example divided by number of options.
outputs = (np.sum(inputs, axis=-1)) / (inputs.shape[-1] * options)

# Create model
M = models.Sequential()
M.add(layers.Dense(10, activation=activations.leaky_relu))
M.add(layers.Dense(20, activation=activations.leaky_relu))
M.add(layers.Dense(20, activation=activations.leaky_relu))
M.add(layers.Dense(1, activation=activations.sigmoid))
M.compile()
M.description()

M.train(inputs, outputs, optimizer='SGD', epochs=2, learning_rate=0.0001)

# Create test data
test_inputs = np.random.randint(0, options + 1, size=[20, num_in])
test_outputs = (np.sum(test_inputs,
                       axis=-1)) / (test_inputs.shape[-1] * options)
outputs = []
# Compare model output to test data output
print("test_output vs calculated output")
Exemple #4
0
M.add(
    layers.Conv2D(16,
                  kernel_size=(5, 5),
                  input_shape=(inputs.shape[-3:]),
                  padding='same',
                  activation=activations.leaky_relu))
M.add(layers.MaxPooling2D((2, 2)))
M.add(
    layers.Conv2D(32,
                  kernel_size=(3, 3),
                  input_shape=(16, 18, 18),
                  padding='same',
                  activation=activations.leaky_relu))
M.add(layers.MaxPooling2D((2, 2)))
M.add(layers.Flatten())
M.add(layers.Dense(144, activation=activations.relu))
M.add(layers.Reshape((1, 12, 12)))

M.compile()
M.description()

# # Load trained network from .npz file
# network_path = 'network_params.npz'
# if path.exists(network_path):
#     print('Loading params from external file...')
#     params = np.load(network_path)
#     for key in params.files:
#         M.params[key] = params[key]

M.train(inputs, targets, optimizer='SGD', epochs=10, learning_rate=0.001)
# # Save the network params to disk
# M.add(layers.Reshape((784), input_shape=(1,28,28)))

M.add(
    layers.Conv2D(25,
                  kernel_size=(3, 3),
                  input_shape=(1, 28, 28),
                  padding="valid",
                  activation=activations.leaky_relu,
                  kernel_constraint=constraints.max_norm(4)))
M.add(layers.MaxPooling2D((2, 2)))
M.add(layers.Flatten())

M.add(
    layers.Dense(100,
                 activation=activations.leaky_relu,
                 kernel_constraint=constraints.max_norm(4)))
M.add(
    layers.Dense(10,
                 activation=activations.softmax,
                 kernel_constraint=constraints.max_norm(4)))

# M.add(layers.Conv2D(64, kernel_size=(3,3), input_shape=(32,14,14), activation=activations.leaky_relu, kernel_constraint=constraints.max_norm(4)))
# M.add(layers.Conv2D(64, kernel_size=(3,3), input_shape=(64,14,14), activation=activations.leaky_relu, kernel_constraint=constraints.max_norm(4)))
# M.add(layers.MaxPooling2D((2,2)))
# M.add(layers.Dropout(0.75))
# M.add(layers.Flatten())
# M.add(layers.Dense(256, activation=activations.leaky_relu, kernel_constraint=constraints.max_norm(4)))
# M.add(layers.Dense(84, activation=activations.leaky_relu, kernel_constraint=constraints.max_norm(4)))
# M.add(layers.Dropout(0.75))
# M.add(layers.Dense(10, activation=activations.softmax, kernel_constraint=constraints.max_norm(4)))