コード例 #1
0
ファイル: actor.py プロジェクト: Lagostra/IT3105
    def __init__(self, game, layers=[], checkpoint=None, format='one_hot', optimizer='adam'):
        self.game = game
        self.format = format
        self.layers = layers
        self.optimizer = optimizer

        self.network = Network(
            [game.state_size(format)] + layers + [game.num_possible_moves()],
            [],
            minibatch_size=50,
            steps=1,
            loss_function='cross_entropy',
            validation_fraction=0,
            test_fraction=0,
            learning_rate=0.001,
            optimizer=optimizer,
            output_functions=[tf.nn.softmax]
        )
        self.network.build()

        if checkpoint:
            self.load_checkpoint(checkpoint)
コード例 #2
0
ファイル: actor.py プロジェクト: Lagostra/IT3105
class Actor:

    def __init__(self, game, layers=[], checkpoint=None, format='one_hot', optimizer='adam'):
        self.game = game
        self.format = format
        self.layers = layers
        self.optimizer = optimizer

        self.network = Network(
            [game.state_size(format)] + layers + [game.num_possible_moves()],
            [],
            minibatch_size=50,
            steps=1,
            loss_function='cross_entropy',
            validation_fraction=0,
            test_fraction=0,
            learning_rate=0.001,
            optimizer=optimizer,
            output_functions=[tf.nn.softmax]
        )
        self.network.build()

        if checkpoint:
            self.load_checkpoint(checkpoint)

    def select_move(self, state, stochastic=False):
        possible_moves = self.game.get_moves(state)
        formatted_state = self.game.format_for_nn(state, format=self.format)
        predictions = self.network.predict([formatted_state])[0]

        predictions = predictions[:len(possible_moves)]
        if not stochastic:
            move = np.argmax(predictions)
            return possible_moves[move]

        predictions = np.array(predictions)
        ps = predictions.sum()
        if predictions.sum() == 0:
            move = np.random.choice(np.arange(0, len(predictions)))
        else:
            predictions = predictions / predictions.sum()
            move = np.random.choice(np.arange(0, len(predictions)), p=predictions)
        return possible_moves[move]

    def save_checkpoint(self, checkpoint):
        self.network.save(checkpoint)

    def load_checkpoint(self, checkpoint):
        self.network.load(checkpoint)
コード例 #3
0
ファイル: train_ann.py プロジェクト: Lagostra/IT3105
            state = game.format_for_nn((board, player), format=state_format)
            result.append((state, probs))

    return result


if __name__ == '__main__':
    data = load_data('model/100x50-500/replays.txt')

    network = Network([game.state_size(state_format)] + layers +
                      [game.num_possible_moves()],
                      data,
                      minibatch_size=500,
                      steps=5000,
                      loss_function='cross_entropy',
                      case_fraction=0.1,
                      validation_fraction=0,
                      validation_interval=1000,
                      test_fraction=0,
                      learning_rate=0.01,
                      optimizer='rmsprop',
                      accuracy_argmax=True,
                      output_functions=[tf.nn.softmax])

    network.build()
    #network.save('model/pre-trained/step-0')
    # for i in range(4):
    #     network.train(plot_results=True)
    #     network.save(f'model/pre-trained/game_{i*50}')
    network.train(plot_results=True)
    network.save('model/manual/test4')
    #network.test()
コード例 #4
0
from nn.network import Network
from sklearn.metrics import accuracy_score
import numpy as np
from sklearn.metrics import confusion_matrix
import tensorflow as tf
from sklearn import metrics

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = np.reshape(x_train, (x_train.shape[0], 28 * 28))
x_test = np.reshape(x_test, (x_test.shape[0], 28 * 28))
y_train = tf.keras.utils.to_categorical(y_train)
x_train = (x_train / 255).astype('float32')
x_test = (x_test / 255).astype('float32')

net = Network()

net.init(input_dimension=784,
         loss_function="cross entropy",
         layers=[{
             "units": 128,
             "activation": "relu",
             "type": "dense"
         }, {
             "units": 64,
             "activation": "relu",
             "type": "dense"
         }, {
             "units": 10,
             "activation": "softmax",
             "type": "dense"
コード例 #5
0
from nn.activation_functions.logistic import Logistic
from nn.cost_functions.quadratic import QuadraticCost
from nn.network import Network
from nn.optimization_strategies.gradient_descent import GradientDescent

if __name__ == '__main__':
    learning_rate = 5
    activation = Logistic()
    cost = QuadraticCost()
    optimization = GradientDescent(learning_rate)
    nn = Network([2, 6, 1], activation, cost, optimization)

    episodes = 10000
    input_sample = [[0, 0], [0, 1], [1, 0], [1, 1]]

    output_sample = [[0], [1], [1], [0]]

    nn.run(episodes, input_sample, output_sample)
コード例 #6
0
    return a

if __name__ == '__main__':
    batch_size = 20

    # A simple strided convnet
    layers = [
        Conv((4, 4, 1, 20), strides=2, activation=lkrelu, filter_init=lambda shp: np.random.normal(size=shp) * np.sqrt(1.0 / (28*28 + 13*13*20)) ),
        Conv((5, 5, 20, 40), strides=2, activation=lkrelu, filter_init=lambda shp:  np.random.normal(size=shp) *  np.sqrt(1.0 / (13*13*20 + 5*5*40)) ),
        Flatten((5, 5, 40)),
        FullyConnected((5*5*40, 100), activation=sigmoid, weight_init=lambda shp: np.random.normal(size=shp) * np.sqrt(1.0 / (5*5*40 + 100.))),
        FullyConnected((100, 10), activation=linear, weight_init=lambda shp: np.random.normal(size=shp) * np.sqrt(1.0 / (110.)))
    ]
    lr = 0.001
    k = 2000
    net = Network(layers, lr=lr, loss=cross_entropy)

    (train_data_X, train_data_Y), v, (tx, ty) = mnist_loader.load_data('./data/mnist.pkl.gz')
    train_data_Y = one_hot(train_data_Y, size=10)
    ty = one_hot(ty, size=10)
    train_data_X = np.reshape(train_data_X, [-1, 28, 28, 1])
    tx = np.reshape(tx, [-1, 28, 28, 1])
    for epoch in xrange(100000):
        shuffled_index = np.random.permutation(train_data_X.shape[0])

        batch_train_X = train_data_X[shuffled_index[:batch_size]]
        batch_train_Y = train_data_Y[shuffled_index[:batch_size]]
        net.train_step((batch_train_X, batch_train_Y))

        loss = np.sum(cross_entropy.compute((net.forward(batch_train_X), batch_train_Y)))
        print 'Epoch: %d loss : %f' % (epoch, loss)
コード例 #7
0
from data.dataset_generator import Generator, number_to_index
from nn.network import Network

numbers = [6, 5, 2]
layers = [784, 100, 3]
net = Network(layers, number_to_index(numbers))

# cria um dataset de 60000 imagens, sendo 50000 para treino e 10000 para teste.
# os números são limitados a 6, 5, 2, que são os correspondentes as 3 iniciais:
# generator = Generator(50000, 10000, numbers, 28)

# cria o dataset com 3 imagens de teste correspondentes as 3 iniciais, em numeros
generator = Generator(0, 3, numbers, 28, True)
dataset = generator.gen()

# para treinar:
# net.grad_descent(dataset[:50000], 200, 10, 3.0, test_data=dataset[50000:])

# carrega uma rede já treinada anteriormente:
net.load('pre-trained-data/biases.npy', 'pre-trained-data/weights.npy')

# porcentagem de acerto:
print((net.evaluate(dataset) / 3) * 100)
コード例 #8
0
             filter_init=lambda shp: np.random.normal(size=shp) * np.sqrt(
                 1.0 / (13 * 13 * 20 + 5 * 5 * 40))),
        Flatten((5, 5, 40)),
        FullyConnected((5 * 5 * 40, 100),
                       activation=relu,
                       weight_init=lambda shp: np.random.normal(size=shp) * np.
                       sqrt(1.0 / (5 * 5 * 40 + 100.))),
        FullyConnected((100, 2),
                       activation=sigmoid,
                       weight_init=lambda shp: np.random.normal(size=shp) * np.
                       sqrt(1.0 / (110.)))
    ]

    lr = 0.000000001

    net = Network(layers, lr=lr, loss=mse)
    # If you want to continue training a network uncomment the line below
    # net = pickle.load(open("network.nn", "rb"))

    train_data_X = np.reshape(train_data_X, [-1, 28, 28, 3])

    try:
        for epoch in range(100000):
            shuffled_index = np.random.permutation(train_data_X.shape[0])

            # Get minibatch of training data
            batch_train_X = train_data_X[shuffled_index[:batch_size]]
            batch_train_Y = train_data_Y[shuffled_index[:batch_size]]
            net.train_step((batch_train_X, batch_train_Y))

            loss = np.sum(
コード例 #9
0
def run_case(n, hyperparameter_file='hyperparameters.csv'):
    """
    Runs the dataset and network defined on line n in the given hyperparameter file.
    :param n: The line of the scenario.
    :param hyperparameter_file: A CSV file containing references to data sets and hyperparameters.
    :return:
    """
    params = pd.read_csv(hyperparameter_file).loc[n - 1]

    dataset = params['Dataset']
    try:
        dataset = eval(dataset)
    except NameError:
        pass

    output_functions = params['Output_functions']
    try:
        output_functions = eval(output_functions)
    except NameError:
        pass

    network = Network(eval(params['Network']),
                      dataset,
                      minibatch_size=int(params['Minibatch_size']),
                      steps=int(params['Steps']),
                      loss_function=params['Loss_function'],
                      output_functions=output_functions,
                      case_fraction=float(params['case_fraction']),
                      validation_fraction=float(params['validation_fraction']),
                      validation_interval=int(params['validation_interval']),
                      test_fraction=float(params['test_fraction']),
                      learning_rate=float(params['learning_rate']),
                      optimizer=params['optimizer'],
                      one_hot_encode_target=bool(
                          params['one_hot_encode_target']))

    network.build()
    network.train()
    network.test()

    # network.mapping_test(10, [0])
    network.mapping_test(dataset, [0], [0])

    network.visualize_weights(weight_layers=[0, 1], bias_layers=[0, 1])