Esempio n. 1
0
def run_monk_folded():
    task = "monk"
    eta = 0.03
    alpha = 0.3
    lambd = 0.00
    eta_decay = 0.0
    epochs = 25
    verbose = True
    grid_search = False

    training_set = "dataset/monk3/monk3train_onehot.csv"
    test_set = "dataset/monk3/monk3test_onehot.csv"
    folds = 4
    train_folded, val_folded = k_fold(get_dataset(training_set), folds)
    nn_to_plot = []
    # architecture --> [hidden layer, nueroni x hidden, eta, alpha, lambd, act hidden layer]
    architecture = [1, 3, eta, alpha, lambd, "sigmoid"]

    for i in range(len(train_folded)):
        print(
            f"KFOLD {i + 1} of {folds} _______________________________________"
        )
        nn = NeuralNet("mean_squared_error")
        nn.init_input_layer(17)
        nn.init_layer(3, 17, "sigmoid")
        nn.init_layer(1, 3, "sigmoid")

        tr = train_folded[i]
        tval = val_folded[i]

        nn.train(task, tr, tval, epochs, eta, alpha, lambd, eta_decay, verbose,
                 grid_search)
        nn_to_plot.append(nn)

    plot_multinetwork(nn_to_plot, eta, alpha, lambd, folds, architecture)
Esempio n. 2
0
def run_cup():
    training_cup = "dataset/blindcup/training_set.csv"

    training_set = get_dataset(training_cup)
    validation_set = get_dataset(training_cup)

    nn = NeuralNet("mean_euclidean_error")
    nn.init_input_layer(10)
    nn.init_layer(23, 10, "tanh")
    nn.init_layer(23, 23, "tanh")
    nn.init_layer(2, 23, "linear")

    task = "cup"
    eta = 0.01
    alpha = 0.80
    lambd = 0.00
    epochs = 250
    eta_decay = 0.00
    nn.train(task, training_set, validation_set, epochs, eta, alpha, lambd,
             eta_decay, True)
    simple_plot(task, nn, eta, alpha)
        return None

device = torch.device('cpu')
with open(teachingFile,'r') as speak:
    intents = json.load(speak)

trainFile = torch.load(trainingFile)

input_size = trainFile["input_size"]
hidden_size = trainFile["hidden_size"]
output_size = trainFile["output_size"]
all_words = trainFile["all_words"]
tags = trainFile["tags"]
model_state = trainFile["model_state"]

model = NeuralNet(input_size,hidden_size,output_size).to(device)
model.load_state_dict(model_state)
model.eval()

def aiReply(userSay):

    chat = tokenize(userSay)
    x = bagOfWords(chat,all_words)
    x = x.reshape(1,x.shape[0])
    x = torch.from_numpy(x)
    output = model(x)
    _,predicted = torch.max(output,dim=1)
    tag = tags[predicted.item()]

    probability = torch.softmax(output,dim=1)
    prob = probability[0][predicted.item()]
Esempio n. 4
0
def run_cup_folded(train_set, val_set):
    nn = NeuralNet("mean_squared_error")
    nn.init_input_layer(10)
    nn.init_layer(23, 10, "tanh")
    nn.init_layer(23, 23, "tanh")
    nn.init_layer(23, 23, "tanh")
    nn.init_layer(2, 23, "linear")

    training_set = train_set
    validation_set = val_set

    task = "cup"
    eta = 0.003
    alpha = 0.2
    lambd = 0.01
    epochs = 100
    nn.train(task, training_set, validation_set, epochs, eta, alpha, False)
    return nn
Esempio n. 5
0
def run_final_model():
    task = "cup"
    eta = 0.001
    alpha = 0.2
    lambd = 0.0001
    eta_decay = 0.000
    epochs = 500
    verbose = True
    grid_search = False

    training_cup = get_dataset("dataset/blindcup/training_set2.csv")
    # architecture --> [hidden layer, nueroni x hidden, eta, alpha, lambd, act hidden layer]
    architecture = [1, 40, eta, alpha, lambd, "tanh"]

    # initializing network
    nn = NeuralNet("mean_euclidean_error")

    # initializing input layer
    nn.init_input_layer(10)

    # adding layers
    nn.init_layer(40, 10, "tanh")
    nn.init_layer(2, 40, "linear")

    # setting weights xavier init
    nn.set_weights_pre_training()

    nn.train_final(training_cup, epochs, eta, alpha, lambd, eta_decay, verbose)
    nn.make_prediction(training_cup)
Esempio n. 6
0
def run_model_cup_kfold():
    task = "cup"
    eta = 0.001
    alpha = 0.2
    lambd = 0.0000
    eta_decay = 0.00
    epochs = 250
    verbose = True
    grid_search = False

    training_cup = "dataset/blindcup/training_set2.csv"
    folds = 1
    train_folded, val_folded = k_fold(get_dataset(training_cup), folds)
    nn_to_plot = []
    # architecture --> [hidden layer, nueroni x hidden, eta, alpha, lambd, act hidden layer]
    architecture = [1, 40, eta, alpha, lambd, "tanh"]

    for i in range(len(train_folded)):
        print(
            f"\nKFOLD {i + 1} of {folds} _______________________________________"
        )

        # initializing network
        nn = NeuralNet("mean_euclidean_error")

        # initializing input layer
        nn.init_input_layer(10)

        # adding layers
        nn.init_layer(40, 10, "tanh")
        nn.init_layer(2, 40, "linear")

        # setting weights xavier init
        nn.set_weights_pre_training()

        tr = train_folded[i]
        tval = val_folded[i]

        nn.train(task, tr, tval, epochs, eta, alpha, lambd, eta_decay, verbose,
                 grid_search)
        nn_to_plot.append(nn)

    plot_multinetwork(nn_to_plot, eta, alpha, lambd, folds, architecture)

    error_kfold_tr = 0
    error_kfold_val = 0
    for network in nn_to_plot:
        error_kfold_tr += network.error_list[-1][1]
        error_kfold_val += network.validation_error_list[-1][1]

    error_kfold_tr = round(error_kfold_tr / len(train_folded), 5)
    error_kfold_val = round(error_kfold_val / len(train_folded), 5)
    print(
        f"\nNN Architecture: Layers: {len(nn_to_plot[0].layer_list)}, Units x hidden layer: {len(nn_to_plot[0].layer_list[1].net)}"
        f"\nHyperparameters: eta: {eta}, alpha: {alpha}, lambda: {lambd}, eta decay: {eta_decay}"
        f"\nAverage final error on training set: {error_kfold_tr}"
        f"\nAverage final error on validat. set: {error_kfold_val}\n")
Esempio n. 7
0
def run_monk():
    nn = NeuralNet("mean_squared_error")
    nn.init_input_layer(17)
    nn.init_layer(3, 17, "sigmoid")
    nn.init_layer(1, 3, "sigmoid")

    train_csv_one_hot = "dataset/monk3/monk3train_onehot.csv"
    test_csv_one_hot = "dataset/monk3/monk3test_onehot.csv"

    training_set = get_dataset(train_csv_one_hot)
    validation_set = get_dataset(test_csv_one_hot)

    task = "monk"
    eta = 0.3
    alpha = 0.6
    lambd = 0.00
    eta_decay = 0.00
    epochs = 150
    nn.set_weights_pre_training()
    nn.train(task,
             training_set,
             validation_set,
             epochs,
             eta,
             alpha,
             lambd,
             eta_decay,
             verbose=True,
             grid_search=False)

    simple_plot(task, nn, eta, alpha)
    def __len__(self):
        return self.n_samples


output_size = len(tags)
input_size = len(x_train[0])

dataset = ChatDataset()

device = torch.device('cpu')
train_loader = DataLoader(dataset=dataset,
                          batch_size=batchSize,
                          shuffle=True,
                          num_workers=0)
model = NeuralNet(input_size, hidden_size, output_size).to(device)

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

for epoch in range(num_epochs):
    for (words, labels) in train_loader:
        words = words.to(device)
        labels = labels.to(dtype=torch.long).to(device)

        output = model(words)
        loss = criterion(output, labels)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()