def __init__(self, env, save_dirs, learning_rate=0.0001):
        BaseModel.__init__(self,
                           input_shape=env.observation_space.shape,
                           num_actions=env.action_space.n,
                           save_dirs=save_dirs)

        self.env = env

        self.blueprint = {
            'conv_layers': 3,
            'filters': [32, 64, 64],
            'kernel_sizes': [(8, 8), (4, 4), (3, 3)],
            'strides': [(4, 4), (2, 2), (1, 1)],
            'paddings': ['valid', 'valid', 'valid'],
            'activations': ['relu', 'relu', 'relu'],
            'dense_units': 512,
            'dense_activation': 'relu'
        }

        self.local_model_save_path = os.path.join(self.save_path,
                                                  'local-wts.h5')
        self.local_model = NeuralNet(input_shape=self.input_shape,
                                     num_actions=self.num_actions,
                                     learning_rate=learning_rate,
                                     blueprint=self.blueprint).model
def run_LSTM_prediction():
    app = wx.App()
    lstmNN = NeuralNet(XML)
    app.MainLoop()
    #if the LSTM has a valid h5 weight file, then load and use it to classify
    if lstmNN.LoadLSTM():
        lstmNN.Predict()
    def __init__(self,
                 env,
                 save_dirs,
                 save_freq=10000,
                 gamma=0.99,
                 batch_size=32,
                 learning_rate=0.0001,
                 buffer_size=10000,
                 learn_start=10000,
                 target_network_update_freq=1000,
                 train_freq=4,
                 epsilon_min=0.01,
                 exploration_fraction=0.1,
                 tot_steps=int(1e7)):
        DDQN.__init__(self,
                      env=env,
                      save_dirs=save_dirs,
                      learning_rate=learning_rate)

        self.gamma = gamma
        self.batch_size = batch_size
        self.learning_rate = learning_rate
        self.buffer_size = buffer_size
        self.learn_start = learn_start
        self.target_network_update_freq = target_network_update_freq
        self.train_freq = train_freq
        self.epsilon_min = epsilon_min
        self.exploration_fraction = exploration_fraction
        self.tot_steps = tot_steps
        self.epsilon = 1.0
        self.exploration = LinearSchedule(schedule_timesteps=int(
            self.exploration_fraction * self.tot_steps),
                                          initial_p=self.epsilon,
                                          final_p=self.epsilon_min)

        self.save_freq = save_freq

        self.replay_buffer = ReplayBuffer(save_dirs=save_dirs,
                                          buffer_size=self.buffer_size,
                                          obs_shape=self.input_shape)

        self.exploration_factor_save_path = os.path.join(
            self.save_path, 'exploration-factor.npz')

        self.target_model_save_path = os.path.join(self.save_path,
                                                   'target-wts.h5')
        self.target_model = NeuralNet(input_shape=self.input_shape,
                                      num_actions=self.num_actions,
                                      learning_rate=learning_rate,
                                      blueprint=self.blueprint).model

        self.show_hyperparams()

        self.update_target()

        self.load()
Esempio n. 4
0
def plot():
    """
    Ploting the matrix of the coupling constants
    """

    n = [400, 10000]
    fig, axarr = plt.subplots(nrows=2, ncols=3)
    cmap_args=dict(vmin=-1., vmax=1., cmap='seismic')

    for i in range(len(n)):
        n_samples = n[i]

        X = Data[0][:n_samples]
        Y = Data[1][:n_samples]

        regs = [None, 'l2','l1']
        Js = np.zeros((3, L**2))

        for j in range(3):

            nn = NeuralNet(X,Y, nodes=[X.shape[1], 1], activations=[None], regularization=regs[j], lamb=0.1)
            nn.TrainNN(epochs = 500, batchSize = 200, eta0 = 0.01, n_print = 50)
            Js[j] = nn.Weights['W1'].flatten()

        J_ols = Js[0].reshape(L,L)
        J_ridge = Js[1].reshape(L,L)
        J_lasso = Js[2].reshape(L,L)

        axarr[i][0].imshow(J_ols,**cmap_args)
        axarr[i][0].set_title('$\\mathrm{OLS}$',fontsize=16)
        axarr[i][0].tick_params(labelsize=16)

        axarr[i][1].imshow(J_ridge,**cmap_args)
        axarr[i][1].set_title('$\\mathrm{Ridge},\ \\lambda=%.4f$' %(0.1),fontsize=16)
        axarr[i][1].tick_params(labelsize=16)

        im=axarr[i][2].imshow(J_lasso,**cmap_args)
        axarr[i][2].set_title('$\\mathrm{LASSO},\ \\lambda=%.4f$' %(0.1),fontsize=16)
        axarr[i][2].tick_params(labelsize=16)

        divider = make_axes_locatable(axarr[i][2])
        cax = divider.append_axes("right", size="5%", pad=0.05)
        cbar=fig.colorbar(im, cax=cax)

        cbar.ax.set_yticklabels(np.arange(-1.0, 1.0+0.25, 0.25),fontsize=14)
        cbar.set_label('$J_{i,j}$',labelpad=-40, y=1.12,fontsize=16,rotation=0)

        #fig.subplots_adjust(right=2.0)
    plt.show()
Esempio n. 5
0
def main():
    l, h, e, trainSetFile, testSetFile = getArgs()
    trainDataSet = arff.load(open(trainSetFile, 'r'))
    testDataSet = arff.load(open(testSetFile, 'r'))
    labels = trainDataSet['attributes'][-1][1]

    fullTrain, fullTest = dataprocess.process(trainDataSet, testDataSet,
                                              labels)

    ann = NeuralNet(h, 1, fullTrain)
    trainInfo = ann.train(fullTrain, e, l)
    for item in trainInfo:
        print('Epoch: {0}\tCross-entropy error: {1}\tCorrectly classified instances: {2}\tMisclassified instances: {3}'.format\
        (item[0], item[1], item[2], item[3]))

    testInfo, correct, wrong = ann.test(fullTest, labels)
    for item in testInfo:
        print('Activation of output unit: {0}\tPredicted class: {1}\tCorrect class: {2}'.format\
        (item[0], item[1], item[2]))
    print('Correctly classified instances: {0}\tMisclassified instances: {1}'.
          format(correct, wrong))
Esempio n. 6
0
    def __init__(self, input_vector_size, subnet_output_vector_size):
        self.subNet = NeuralNet(no_of_in_nodes=input_vector_size,
                                no_of_out_nodes=subnet_output_vector_size,
                                no_of_hidden_nodes=60,
                                learning_rate=0.1)

        self.superNet = NeuralNet(no_of_in_nodes=self.subNet.no_of_out_nodes,
                                  no_of_out_nodes=2,
                                  no_of_hidden_nodes=15,
                                  learning_rate=0.1)

        #TODO: This is a placeholder; it's supposed to grow as the alternet learns
        self.number_of_alternet_labels = 26
        #Placeholder alternate classifier
        self.alterNet = NeuralNet(no_of_in_nodes=input_vector_size,
                                  no_of_out_nodes=26,
                                  no_of_hidden_nodes=60,
                                  learning_rate=0.1)

        #Offset the alterNet labelKeys by the number of subNet labels so we don't overlap
        self.alterNet.set_label_key(
            np.array([
                i + subnet_output_vector_size
                for i in range(self.number_of_alternet_labels)
            ]))
Esempio n. 7
0
def reg():
    """
    Doing the linear regression using
    a neural network
    """

    lambdas = np.logspace(-4, 2, 7)
    #lambdas = [0.01, 0.1]
    n = len(lambdas)
    train_mse = np.zeros((3,n))
    test_mse = np.zeros((3,n))
    regs = [None, 'l1', 'l2']

    for j in range(3):
        for i in range(n):
            nn = NeuralNet(X,Y, nodes=[X.shape[1], 1], activations=[None], regularization=regs[j], lamb=lambdas[i])
            nn.TrainNN(epochs = 1000, batchSize = 200, eta0 = 0.01, n_print = 10)

            ypred_test = nn.feed_forward(nn.xTest, isTraining=False)
            mse_test = nn.cost_function(nn.yTest, ypred_test)

            test_mse[j,i] = mse_test

            if j == 0:
                test_mse[0].fill(test_mse[0,0])
                break


    plt.semilogx(lambdas, test_mse.T)
    plt.xlabel(r'$\lambda$')
    plt.ylabel('MSE')
    plt.title("MSE on Test Set, %i samples" %(n_samples))
    plt.legend(['No Reg', 'L1', 'L2'])
    plt.grid(True)
    plt.ylim([0, 25])
    plt.show()
class DDQN(BaseModel):
    def __init__(self, env, save_dirs, learning_rate=0.0001):
        BaseModel.__init__(self,
                           input_shape=env.observation_space.shape,
                           num_actions=env.action_space.n,
                           save_dirs=save_dirs)

        self.env = env

        self.blueprint = {
            'conv_layers': 3,
            'filters': [32, 64, 64],
            'kernel_sizes': [(8, 8), (4, 4), (3, 3)],
            'strides': [(4, 4), (2, 2), (1, 1)],
            'paddings': ['valid', 'valid', 'valid'],
            'activations': ['relu', 'relu', 'relu'],
            'dense_units': 512,
            'dense_activation': 'relu'
        }

        self.local_model_save_path = os.path.join(self.save_path,
                                                  'local-wts.h5')
        self.local_model = NeuralNet(input_shape=self.input_shape,
                                     num_actions=self.num_actions,
                                     learning_rate=learning_rate,
                                     blueprint=self.blueprint).model

    def load_mdl(self):
        if os.path.isfile(self.local_model_save_path):
            self.local_model.load_weights(self.local_model_save_path)
            print('Loaded Local Model...')
        else:
            print('No existing Local Model found...')

    def load(self):
        self.load_mdl()
Esempio n. 9
0
def nn_resample(Pca: bool = 0,
                smote: bool = 0,
                over: bool = 0,
                under: bool = 0,
                X: np.ndarray = X,
                Y: np.ndarray = Y) -> None:
    np.random.seed(42)
    if Pca:
        pca = PCA(n_components=14)
        X = pca.fit_transform(X)

    ##Initialize network###
    node1 = X.shape[1]
    nn = NeuralNet(X,
                   Y.flatten(),
                   nodes=[node1, 3, 2],
                   activations=['tanh', None],
                   cost_func='log',
                   regularization='l2',
                   lamb=0.001)

    ###Split Data###
    nn.split_data(frac=0.5, shuffle=True)

    ##Choose resampling teqnique##
    if smote:
        # sm = SMOTE(random_state=42, sampling_strategy=1.0)
        sm = SMOTE()
        nn.xTrain, nn.yTrain = sm.fit_resample(nn.xTrain, nn.yTrain)

    elif under:
        resample = rs(nn.xTrain, nn.yTrain)
        nn.xTrain, nn.yTrain = resample.Under()

    elif over:
        resample = rs(nn.xTrain, nn.yTrain)
        nn.xTrain, nn.yTrain = resample.Over()
    ##Train network##
    nn.TrainNN(epochs=100, batchSize=200, eta0=0.01, n_print=50)
Esempio n. 10
0
import numpy as np
from read_data import get_data
import sys
sys.path.append("network/")
from NN import NeuralNet

X, Y = get_data(normalized=False, standardized=True, file='droppedX6-X11.csv')
nn = NeuralNet(X,
               Y.flatten(),
               nodes=[18, 50, 50, 2],
               activations=['tanh', 'tanh', None],
               cost_func='log',
               regularization='l2',
               lamb=0.001)

nn.split_data(frac=0.5, shuffle=True, resample=True)
nn.TrainNN(epochs=1000, batchSize=200, eta0=0.01, n_print=10)
Esempio n. 11
0
"""
Neural network calcualting logistic regression
"""
import sys
sys.path.append('network')
sys.path.append('../')
sys.path.append('../../')
import numpy as np
from fetch_2D_data import fetch_data
from NN import NeuralNet

X, Y, X_crit, Y_crit = fetch_data()

nn = NeuralNet(X,
               Y,
               nodes=[X.shape[1], 2],
               activations=[None],
               cost_func='log')
nn.TrainNN(epochs=200, batchSize=130, eta0=0.001, n_print=20)

ypred_crit = nn.feed_forward(X_crit, isTraining=False)
critError = nn.cost_function(Y_crit, ypred_crit)
critAcc = nn.accuracy(Y_crit, ypred_crit)

print("Critical error: %g,  Critical accuracy: %g" % (critError, critAcc))
Esempio n. 12
0
run on all the data available in
network/fetch_2D_data"""

import numpy as np
import sys
sys.path.append('network')
sys.path.append('../')
sys.path.append('../../')
from fetch_2D_data import fetch_data
from NN import NeuralNet

X, Y, X_crit, Y_crit = fetch_data()
### LOG-REG CASE
# nn = NeuralNet(X,Y, nodes = [X.shape[1], 2], activations = [None], cost_func='log')
# nn.split_data(frac=0.5, shuffle=True)
# nn.feed_forward(nn.xTrain)
# nn.backpropagation()
# nn.TrainNN(epochs = 100, eta = 0.001, n_print=5)

# 100% ACCURACY MADDAFAKKA
nn = NeuralNet(X,Y, nodes = [X.shape[1],10,2], activations = ['tanh',None],\
                cost_func='log', regularization='l2', lamb=0.01)
nn.split_data(frac=0.5, shuffle=True)
nn.TrainNN(epochs=200, eta0=0.01, n_print=5)

ypred_crit = nn.feed_forward(X_crit, isTraining=False)
critError = nn.cost_function(Y_crit, ypred_crit)
critAcc = nn.accuracy(Y_crit, ypred_crit)

print("Critical error: %g,  Critical accuracy: %g" % (critError, critAcc))
Esempio n. 13
0
def best_architecture():
    """
    Finding best architectures for neural network
    """

    #######################################################
    ###############Defining parameters#####################
    #######################################################
    # lambdas = np.logspace(-4, 0, 5)
    lambdas = [0.01]
    nodes = [5]
    regularizations = [None]
    n_samples = [10, 20]
    activation_functions = [['relu', None]]

    df = pd.DataFrame()

    ########################################################
    ################Fetching Data###########################
    ########################################################
    X, Y, X_critical, Y_critical = fetch_data()
    X, Y = shuffle(X, Y)
    X_critical, Y_critical = shuffle(X_critical, Y_critical)

    for sample in n_samples:
        print(sample)
        X_train = X[:sample]; Y_train = Y[:sample]
        X_test = X[sample:2*sample]; Y_test = Y[sample:2*sample]
        X_crit = X_critical[:sample]; Y_crit = Y_critical[:sample]
        for reg in regularizations:
            print(reg)
            for activation in activation_functions:
                print(activation)
                for n in nodes:
                    print(n)
                    node = [X.shape[1], 2]
                    node[1:1] = [n]*(len(activation)-1)
                    print(node)
                    for lamb in lambdas:
                        print(lamb)
                        nn = NeuralNet(
                                    X_train,
                                    Y_train,
                                    nodes = node,
                                    activations = activation,
                                    cost_func='log',
                                    regularization=reg,
                                    lamb=lamb)
                        nn.TrainNN(epochs = 100)

                        ypred_train = nn.feed_forward(X_train, isTraining=False)
                        ypred_test = nn.feed_forward(X_test, isTraining=False)
                        ypred_crit = nn.feed_forward(X_crit, isTraining=False)

                        df = df.append({
                                'Sample size': sample,
                                'Lambda': lamb,
                                'Regularization': reg,
                                'Nodes': n,
                                'Activation': (len(activation)-1)*activation[0],
                                'Train error': nn.cost_function(Y_train, ypred_train),
                                'Test error':  nn.cost_function(Y_test, ypred_test),
                                'Critical error': nn.cost_function(Y_crit, ypred_crit),
                                'Train accuracy':nn.accuracy(Y_train, ypred_train),
                                'Test accuracy': nn.accuracy(Y_test, ypred_test),
                                'Critical accuracy': nn.accuracy(Y_crit, ypred_crit)
                                }, ignore_index=True)
    df.to_csv('best_architecture.csv', index_label='Index')
Esempio n. 14
0
from MTCS import MonteCarloTreeSearch

# Given the path of the assembly task
fpath = './task_1.xlsx'

# Creat the assembly chessboard via AssembleTask
at1 = AssembleTask(fpath)

# Set random seed
np.random.seed(seed=999)

# Create neural network for robot and human
# Note: the architecure of the neural network
#       is given inside the class, one should
#       go to NN.py if changes are needed
robot_net = NeuralNet(at1.h, at1.w, at1.d, 16, 1, value_norm=110)
human_net = NeuralNet(at1.h, at1.w, at1.d, 16, 1, value_norm=90)

# Create the MTCS object
mtcs1 = MonteCarloTreeSearch(MaxDepth=3,
                             MaxSearches=30,
                             MaxIters=10,
                             c_puct=100,
                             atask=at1,
                             robot_net=robot_net,
                             human_net=human_net,
                             switch=5,
                             tau=1000,
                             data_size=200
                             )
Esempio n. 15
0
        one_vs_all=one_vs_all)

if (one_trace):
    input_channels = 1
else:
    input_channels = 3
for i in range(numItera):
    random_st = 42 * i

    if (conv):
        model = ConvNeuralNet2Dense(input_channels=input_channels,
                                    units=units_CNN,
                                    output_size=output_size)
    else:
        model = NeuralNet(input_size=inputs.shape[1],
                          units=units_NN,
                          output_size=output_size)

    X_train, X_val, y_train, y_val = train_test_split(inputs,
                                                      labels,
                                                      test_size=0.2,
                                                      random_state=random_st)
    # w = torch.Tensor([12.0,0.5])
    loss = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(
        model.parameters(),
        lr=0.001,
        betas=(0.09, 0.999),  # eps=1e-08,
        weight_decay=0.0)
    dataset_train = Dataset(X_train, y_train)
    training_generator = data.DataLoader(dataset_train, **params)
Esempio n. 16
0
import sys
sys.path.append("network/")
from NN import NeuralNet
from metrics import gain_chart, prob_acc

#X, Y = get_data(normalized = False, standardized = False)
X, Y = get_data()

#with open("nn_arch.txt", "w+") as f:

    f.write("Activation | Hidden layers | Nodes | Area Test | R2 Test | Error rate \n")
    f.write("-"*70)

    for act in ['tanh', 'sigmoid', 'relu']:
        for size in [5, 10, 20, 50, 100]:
            for n_lay in [1, 2, 3]:
                nn = NeuralNet(X, Y.flatten(), nodes = [23] + [size]*n_lay + [2], \
                    activations = [act]*n_lay + [None], cost_func = 'log')
                nn.split_data(frac = 0.5, shuffle = True)
                nn.TrainNN(epochs = 2000, batchSize = 200, eta0 = 0.01, n_print = 100)

                ypred_test = nn.feed_forward(nn.xTest, isTraining=False)
                acc = nn.accuracy(nn.yTest, ypred_test)
                err_rate = 1 - acc/100
                area = gain_chart(nn.yTest, ypred_test, plot=False)
                R2 = prob_acc(nn.yTest, ypred_test, plot=False)

                f.write("\n  %s  |  %i  |  %i  |  %.5f  |  %.5f  |  %.3f "\
                        %(act, n_lay, size, area, R2, err_rate))
        f.write("\n" + "-"*70)
Esempio n. 17
0
class MetaNet:

    #TODO: Think about how to generalize this so that it can be built up
    def __init__(self, input_vector_size, subnet_output_vector_size):
        self.subNet = NeuralNet(no_of_in_nodes=input_vector_size,
                                no_of_out_nodes=subnet_output_vector_size,
                                no_of_hidden_nodes=60,
                                learning_rate=0.1)

        self.superNet = NeuralNet(no_of_in_nodes=self.subNet.no_of_out_nodes,
                                  no_of_out_nodes=2,
                                  no_of_hidden_nodes=15,
                                  learning_rate=0.1)

        #TODO: This is a placeholder; it's supposed to grow as the alternet learns
        self.number_of_alternet_labels = 26
        #Placeholder alternate classifier
        self.alterNet = NeuralNet(no_of_in_nodes=input_vector_size,
                                  no_of_out_nodes=26,
                                  no_of_hidden_nodes=60,
                                  learning_rate=0.1)

        #Offset the alterNet labelKeys by the number of subNet labels so we don't overlap
        self.alterNet.set_label_key(
            np.array([
                i + subnet_output_vector_size
                for i in range(self.number_of_alternet_labels)
            ]))

    #superNet is the core of the MetaNet instance, but sub and alter can be swapped out
    def setSubNet(self, subNet):
        self.subNet = subNet

    def setAlterNet(self, alterNet):
        self.alterNet = alterNet

    #Single Instance Training; returns predictions before altering weights
    def trainSubNet(self, img, label):
        print(np.argmax(self.subNet.train(img, label)))
        return self.subNet.labelKey[np.argmax(self.subNet.train(img, label))]

    #TODO: May become defunct if alternet is designed to cluster (eg. learn unsupervised)
    def trainAlterNet(self, img, label):
        #print(self.alterNet.labelKey[np.argmax(self.alterNet.train(img, label))])
        return self.alterNet.labelKey[np.argmax(self.alterNet.train(
            img, label))]

    #Train super with the bit of input data.
    #Returns prediction as (img_label, meta_label) tuple
    def trainSuperNet(self, img, img_label, super_label):

        subNet_outVector = self.subNet.run(img)
        superNet_outVector = self.superNet.train(subNet_outVector.T,
                                                 super_label)

        return np.argmax(superNet_outVector)

    #Return result without altering weights
    #TODO: Should this be a tuple as well to keep it in line with train?
    def run(self, img):
        subNet_outVector = self.subNet.run(img)
        superNet_outVector = self.superNet.run(subNet_outVector.T)
        if np.argmax(superNet_outVector) == 1:
            return self.subNet.labelKey[np.argmax(subNet_outVector)]
        else:
            return self.alterNet.labelKey[np.argmax(self.alterNet.run(img))]

    #TODO: Generalize this so it generates a MetaNet with a sub, meta, and alter net
    def generateChild(self, training_set):

        #Output vector size is equal to vector size of current network
        #As we create new categories each "generation" of network will have more outnodes
        child = MetaNet(input_vector_size=self.subNet.no_of_in_nodes,
                        subnet_output_vector_size=self.subNet.no_of_out_nodes +
                        self.alterNet.no_of_out_nodes)

        accuracy = []
        for datum in training_set:
            img, label = datum
            parentResult = self.run(img)
            if (parentResult != label):
                accuracy.append(0)
            else:
                accuracy.append(1)
            child.subNet.train(img, parentResult)

        return child, accuracy
class DDQNLearner(DDQN):
    def __init__(self,
                 env,
                 save_dirs,
                 save_freq=10000,
                 gamma=0.99,
                 batch_size=32,
                 learning_rate=0.0001,
                 buffer_size=10000,
                 learn_start=10000,
                 target_network_update_freq=1000,
                 train_freq=4,
                 epsilon_min=0.01,
                 exploration_fraction=0.1,
                 tot_steps=int(1e7)):
        DDQN.__init__(self,
                      env=env,
                      save_dirs=save_dirs,
                      learning_rate=learning_rate)

        self.gamma = gamma
        self.batch_size = batch_size
        self.learning_rate = learning_rate
        self.buffer_size = buffer_size
        self.learn_start = learn_start
        self.target_network_update_freq = target_network_update_freq
        self.train_freq = train_freq
        self.epsilon_min = epsilon_min
        self.exploration_fraction = exploration_fraction
        self.tot_steps = tot_steps
        self.epsilon = 1.0
        self.exploration = LinearSchedule(schedule_timesteps=int(
            self.exploration_fraction * self.tot_steps),
                                          initial_p=self.epsilon,
                                          final_p=self.epsilon_min)

        self.save_freq = save_freq

        self.replay_buffer = ReplayBuffer(save_dirs=save_dirs,
                                          buffer_size=self.buffer_size,
                                          obs_shape=self.input_shape)

        self.exploration_factor_save_path = os.path.join(
            self.save_path, 'exploration-factor.npz')

        self.target_model_save_path = os.path.join(self.save_path,
                                                   'target-wts.h5')
        self.target_model = NeuralNet(input_shape=self.input_shape,
                                      num_actions=self.num_actions,
                                      learning_rate=learning_rate,
                                      blueprint=self.blueprint).model

        self.show_hyperparams()

        self.update_target()

        self.load()

    def update_exploration(self, t):
        self.epsilon = self.exploration.value(t)

    def update_target(self):
        self.target_model.set_weights(self.local_model.get_weights())

    def remember(self, obs, action, rew, new_obs, done):
        self.replay_buffer.add(obs, action, rew, new_obs, done)

    def step_update(self, t):
        hist = None

        if t <= self.learn_start:
            return hist
        if t % self.train_freq == 0:
            hist = self.learn()
        if t % self.target_network_update_freq == 0:
            self.update_target()
        return hist

    def act(self, obs):
        if np.random.rand() < self.epsilon:
            return self.env.action_space.sample()
        q_vals = self.local_model.predict(
            np.expand_dims(obs, axis=0).astype(float) / 255, batch_size=1)
        return np.argmax(q_vals[0])

    def learn(self):
        if self.replay_buffer.meta_data['fill_size'] < self.batch_size:
            return

        curr_obs, action, reward, next_obs, done = self.replay_buffer.get_minibatch(
            self.batch_size)
        target = self.local_model.predict(curr_obs.astype(float) / 255,
                                          batch_size=self.batch_size)

        done_mask = done.ravel()
        undone_mask = np.invert(done).ravel()

        target[done_mask,
               action[done_mask].ravel()] = reward[done_mask].ravel()

        Q_target = self.target_model.predict(next_obs.astype(float) / 255,
                                             batch_size=self.batch_size)
        Q_future = np.max(Q_target[undone_mask], axis=1)

        target[undone_mask, action[undone_mask].ravel(
        )] = reward[undone_mask].ravel() + self.gamma * Q_future

        hist = self.local_model.fit(curr_obs.astype(float) / 255,
                                    target,
                                    batch_size=self.batch_size,
                                    verbose=0).history
        return hist

    def load_mdl(self):
        super().load_mdl()
        if os.path.isfile(self.target_model_save_path):
            self.target_model.load_weights(self.target_model_save_path)
            print('Loaded Target Model...')
        else:
            print('No existing Target Model found...')

    def save_mdl(self):
        self.local_model.save_weights(self.local_model_save_path)
        print('Local Model Saved...')
        self.target_model.save_weights(self.target_model_save_path)
        print('Target Model Saved...')

    def save_exploration(self):
        np.savez(self.exploration_factor_save_path, exploration=self.epsilon)
        print('Exploration Factor Saved...')

    def load_exploration(self):
        if os.path.isfile(self.exploration_factor_save_path):
            with np.load(self.exploration_factor_save_path) as f:
                self.epsilon = np.asscalar(f['exploration'])
            print('Exploration Factor Loaded...')
        else:
            print('No existing Exploration Factor found...')

    def save(self, t, logger):
        ep = logger.data['episode']
        if (self.save_freq is not None and t > self.learn_start and ep > 100
                and t % self.save_freq == 0):
            if logger.update_best_score():
                logger.save_state()
                self.save_mdl()
                self.save_exploration()
                self.replay_buffer.save()

    def load(self):
        self.load_mdl()
        self.load_exploration()
        self.replay_buffer.load()

    def show_hyperparams(self):
        print('Discount Factor (gamma): {}'.format(self.gamma))
        print('Batch Size: {}'.format(self.batch_size))
        print('Replay Buffer Size: {}'.format(self.buffer_size))
        print('Training Frequency: {}'.format(self.train_freq))
        print('Target network update Frequency: {}'.format(
            self.target_network_update_freq))
        print('Replay start size: {}'.format(self.learn_start))
Esempio n. 19
0
import numpy as np
from game import Game
from NN import NeuralNet
import json

numLayers = int(input("# Layers:"))
nodesPerLayer = int(input("# Nodes:"))
forceValidMoves = (input("Force Valid Moves? ") == "Y");

filename = "NetProgress_"+str(numLayers)+"x"+str(nodesPerLayer)+"_"+str(forceValidMoves)+".json"

population = []
with open(filename) as f:
    x = json.load(f)
    for y in x:
        population.append(NeuralNet(17, 4, numLayers, nodesPerLayer, y))

testing = population[0]

numMoves = 0
game = Game()
while True:
    #Pick the move with the highest rating (+1 is the bias)
    moveChoices = testing.run(game.getLogState()+[1])
    s = sorted(zip(moveChoices, [0,1,2,3]), reverse=True)
    moveChoices = [x[1] for x in s]

    print(game)

    input("")
    print("")
Esempio n. 20
0
def plot_regularization():
    """
    Ploting different models as a function of regularization strength
    """
    #######################################################
    ###############Defining parameters#####################
    #######################################################
    lambs = np.logspace(-4, 0, 5)
    # n_samples =[100, 1000, 4000, 10000]
    n_samples = [3000]
    # nodes = [10]
    nodes=[50]
    #######################################################
    ###############Fetching Data###########################
    #######################################################
    X, Y, X_critical, Y_critical = fetch_data()
    X, Y = shuffle(X, Y)
    X_critical, Y_critical = shuffle(X_critical, Y_critical)

    for node in nodes:
        for sample in n_samples:
            X_train = X[:sample]; Y_train = Y[:sample]
            X_test = X[sample:2*sample]; Y_test = Y[sample:2*sample]
            X_crit = X_critical[:sample]; Y_crit = Y_critical[:sample]


            #######################################################
            ###########Dictionaries for ploting####################
            #######################################################
            errors = {'ols': {'Train': [], 'Test': [], 'Crit': []},
                      'l1':  {'Train': [], 'Test': [], 'Crit': []},
                      'l2':  {'Train': [], 'Test': [], 'Crit': []}}


            accuracies = {'ols': {'Train': [], 'Test': [], 'Crit': []},
                      'l1':  {'Train': [], 'Test': [], 'Crit': []},
                      'l2':  {'Train': [], 'Test': [], 'Crit': []}}


            for lamb in lambs:
                #######################################################
                ###########Initializing networks#######################
                #######################################################
                nn = NeuralNet( X_train, Y_train, nodes = [X.shape[1],  node, 2],
                        activations = ['sigmoid', None], cost_func='log')
                nn_l1 = NeuralNet( X_train, Y_train, nodes = [X.shape[1],  node, 2],
                        activations = ['sigmoid', None], cost_func='log', regularization='l1', lamb=lamb)
                nn_l2 = NeuralNet( X_train, Y_train, nodes = [X.shape[1],  node, 2],
                        activations = ['sigmoid', None], cost_func='log', regularization='l2', lamb=lamb)

                #######################################################
                ###########Spliting data#######################
                #######################################################
                nn.split_data(frac=0.5, shuffle=True)
                nn_l1.split_data(frac=0.5, shuffle=True)
                nn_l2.split_data(frac=0.5, shuffle=True)

                #######################################################
                ###########Training network#######################
                #######################################################

                nn.TrainNN(epochs = 250, eta0 = 0.05, n_print=250)
                nn_l1.TrainNN(epochs = 250, eta0 = 0.05, n_print=250)
                nn_l2.TrainNN(epochs = 250, eta0 = 0.05, n_print=250)


                #######################################################
                ###########Error and accuracies ols#######################
                #######################################################
                ypred_train = nn.feed_forward(X_train, isTraining=False)
                ypred_test = nn.feed_forward(X_test, isTraining=False)
                ypred_crit = nn.feed_forward(X_crit, isTraining=False)
                errors['ols']['Train'].append(nn.cost_function(Y_train, ypred_train))
                errors['ols']['Test'].append(nn.cost_function(Y_test, ypred_test))
                errors['ols']['Crit'].append(nn.cost_function(Y_crit, ypred_crit))
                accuracies['ols']['Train'].append(nn.accuracy(Y_train, ypred_train))
                accuracies['ols']['Test'].append(nn.accuracy(Y_test, ypred_test))
                accuracies['ols']['Crit'].append(nn.accuracy(Y_crit, ypred_crit))

                #######################################################
                ###########Error and accuracies l1#######################
                #######################################################
                ypred_train = nn_l1.feed_forward(X_train, isTraining=False)
                ypred_test = nn_l1.feed_forward(X_test, isTraining=False)
                ypred_crit = nn_l1.feed_forward(X_crit, isTraining=False)
                errors['l1']['Train'].append(nn_l1.cost_function(Y_train, ypred_train))
                errors['l1']['Test'].append(nn_l1.cost_function(Y_test, ypred_test))
                errors['l1']['Crit'].append(nn_l1.cost_function(Y_crit, ypred_crit))
                accuracies['l1']['Train'].append(nn_l1.accuracy(Y_train, ypred_train))
                accuracies['l1']['Test'].append(nn_l1.accuracy(Y_test, ypred_test))
                accuracies['l1']['Crit'].append(nn_l1.accuracy(Y_crit, ypred_crit))

                #######################################################
                ###########Error and accuracies l2#######################
                #######################################################
                ypred_train = nn_l2.feed_forward(X_train, isTraining=False)
                ypred_test = nn_l2.feed_forward(X_test, isTraining=False)
                ypred_crit = nn_l2.feed_forward(X_crit, isTraining=False)
                errors['l2']['Train'].append(nn_l2.cost_function(Y_train, ypred_train))
                errors['l2']['Test'].append(nn_l2.cost_function(Y_test, ypred_test))
                errors['l2']['Crit'].append(nn_l2.cost_function(Y_crit, ypred_crit))
                accuracies['l2']['Train'].append(nn_l2.accuracy(Y_train, ypred_train))
                accuracies['l2']['Test'].append(nn_l2.accuracy(Y_test, ypred_test))
                accuracies['l2']['Crit'].append(nn_l2.accuracy(Y_crit, ypred_crit))


            datasetnames = ['Train', 'Test']
            errfig, errax = plt.subplots()
            accfig, accax = plt.subplots()
            colors = ['#1f77b4', '#ff7f0e', '#2ca02c']
            linestyles = ['-', '--']
            for i, key in enumerate(accuracies):
                for j, name in enumerate(datasetnames):
                    errax.set_xlabel(r'$\lambda$')
                    errax.set_ylabel('Error')
                    errax.semilogx(lambs[:-2], errors[str(key)][str(name)][:-2],
                            color=colors[i], linestyle=linestyles[j], label=str(key).capitalize()+'_'+str(name))
                    errax.legend()



                    accax.set_xlabel(r'$\lambda$')
                    accax.set_ylabel('Accuracy')
                    accax.semilogx(lambs, accuracies[str(key)][str(name)],
                            color=colors[i], linestyle=linestyles[j], label=str(key).capitalize()+'_'+str(name))
                    accax.legend()




            critfig, critax = plt.subplots()
            critax.set_xlabel(r'$\lambda$')
            critax.set_ylabel('Accuracy')
            for i, key in enumerate(accuracies):
                critax.semilogx(lambs, accuracies[str(key)]['Crit'],
                     label=str(key).capitalize()+'_Crit')
                critax.legend()
            plt.show()
Esempio n. 21
0
y_test = y[train_len + val_len:]

print("x_train len : [ " + str(len(x_train)) + " ]")
print("x_val len  : [ " + str(len(x_val)) + " ]")
print("x_test len : [ " + str(len(x_test)) + " ]")

train_size = x_train.shape[0]
batch_size = 20
iters_num = 10000
learning_rate = 0.01

train_loss_list = []
val_loss_list = []
iter_per_epoch = max(train_size / batch_size, 1)

network = NeuralNet(input_size=4, hidden_size=5, output_size=3)
print("Training...")
for _ in range(iters_num):
    grad = network.propagation(x_train, y_train)

    for key in ('W1', 'b1', 'W2', 'b2'):
        network.params[key] -= learning_rate * grad[key]

    if _ % 1000 == 0:
        train_cost = network.loss(x_train, y_train)
        val_cost = network.loss(x_val, y_val)
        train_loss_list.append(train_cost)
        val_loss_list.append(val_cost)

    if _ % 1000 == 0:
        train_cost2 = network.loss(x_train, y_train)
Esempio n. 22
0
def plot_convergence():
    """
    Ploting the convergence rate of the 6 best models
    calculated in best_architectures. 
    """

    ########################################################
    ################Fetching Data###########################
    ########################################################
    sample = 10000
    X, Y, X_critical, Y_critical = fetch_data()
    X, Y = shuffle(X, Y)
    X_critical, Y_critical = shuffle(X_critical, Y_critical)

    X_train = X[:sample]; Y_train = Y[:sample]
    X_test = X[sample:2*sample]; Y_test = Y[sample:2*sample]
    X_crit = X_critical[:sample]; Y_crit = Y_critical[:sample]


    #######################################################
    ###############Defining parameters#####################
    #######################################################

    best_architectures = [['tanh', None], ['tanh', None], ['tanh', None],
            ['tanh','tanh', None], ['tanh', 'tanh', 'tanh', None],
            ['sigmoid', 'sigmoid', None]]
    nodes = [[X.shape[1], 100, 2], [X.shape[1], 50, 2], [X.shape[1], 10, 2],
            [X.shape[1], 100, 100, 2], [X.shape[1], 100, 100, 100, 2],
            [X.shape[1], 100, 100, 2]]


    regularizations = ['l2', 'l2', 'l2', 'l2', 'l1', None]
    lambdas = [0.1, 0.1, 0.1, 0.1, 0.001, 0.01]

    df = pd.DataFrame()
    convergence_dict = {}
    walltime_list = []
    i = 0
    for activation, lamb, node, reg in zip(best_architectures, lambdas, nodes, regularizations):

        nn = NeuralNet(
                    X_train,
                    Y_train,
                    nodes = node,
                    activations = activation,
                    cost_func='log',
                    regularization=reg,
                    lamb=lamb)

        start = time()
        nn.TrainNN(epochs = 100, n_print=1)
        stop = time()
        convergence = nn.convergence_rate
        walltime = (stop - start)
        convergence_dict[activation[0]*(len(activation)-1)+str(node[1])] = convergence['Test Accuracy']
        walltime_list.append(walltime)
        i += 1
    fig, ax = plt.subplots()
    for key in convergence_dict:
        ax.plot(np.arange(101), convergence_dict[key], label=key)
    ax.set_xlabel('Epochs')
    ax.set_ylabel('Accuracy')
    plt.legend()
    # plt.savefig('../plots/convergence_rate.png')

    figb, bx = plt.subplots()
    names = list(convergence_dict.keys())
    bx.bar(np.arange(len(walltime_list)), walltime_list, tick_label=names)
    bx.set_ylabel('Walltime [s]')
    plt.xticks(rotation=-20)
    plt.savefig('../plots/walltime.png')
    plt.tight_layout()
    plt.show()
Esempio n. 23
0
                    if result != -1:
                        break
                    if z + 1 == len(moveChoices):
                        return -10 + numMoves + (3 *
                                                 np.log2(np.amax(game.board)))
            else:
                if (printBoard):
                    print("Fail End")
                    print(game)
                return -10 + numMoves + (3 * np.log2(np.amax(game.board)))
        if result == 0:
            if (printBoard):
                print("Normal End")
                print(game)
            return numMoves + (3 * np.log2(np.amax(game.board)))
        numMoves += 1


#Tries to import the progress of existing neural net
filename = "NetProgress_" + str(numLayers) + "x" + str(
    nodesPerLayer) + "_" + str(forceValidMoves) + ".json"
fitnesses = []
with open(filename) as f:
    x = json.load(f)
    for weights in x:
        net = NeuralNet(17, 4, numLayers, nodesPerLayer, weights)
        fitnesses.append(fitnessFunction(net))

plt.plot(sorted(fitnesses))
plt.show()
Esempio n. 24
0
                               output_size=output_size,
                               input_size=[batch_size, input_channels, 200])

            conv_blocks = [block1, block2, block3]

            model = ConvNeuralNet2Dense3Tanks(
                input_channels=input_channels,
                units=units_CNN,
                output_size=output_size,
                conv_blocks=conv_blocks,
                input_size=[batch_size, input_channels, 200],
                exo_size=[],
                exo=False)
    else:
        model = NeuralNet(input_size=inputs.shape[2],
                          units=units_NN,
                          output_size=output_size)
    model = model.cuda()

    if (three_convs):
        optimizer = torch.optim.SGD(
            filter(lambda p: p.requires_grad, model.parameters()),
            lr=0.01,  #eps=1e-08,
            momentum=0.9000,
            weight_decay=0.0000)
    else:
        optimizer = torch.optim.SGD(
            model.parameters(),
            lr=0.01,  #eps=1e-08,
            momentum=0.9000,
            weight_decay=0.0000)