def model_fn(model_dir):
    """Load the PyTorch model from the `model_dir` directory."""
    print("Loading model.")

    # First, load the parameters used to create the model.
    model_info = {}
    model_info_path = os.path.join(model_dir, 'model_info.pth')
    with open(model_info_path, 'rb') as f:
        model_info = torch.load(f)

    print("model_info: {}".format(model_info))

    # Determine the device and construct the model.
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = NeuralNet(model_info['input_dim'], model_info['hidden_dim'], model_info['output_dim'])

    # Load the stored model parameters.
    model_path = os.path.join(model_dir, 'model.pth')
    with open(model_path, 'rb') as f:
        model.load_state_dict(torch.load(f))

    # prep for testing
    model.to(device).eval()

    print("Done loading model.")
    return model
Example #2
0
def train(args, labeled, resume_from, ckpt_file):
    print("========== In the train step ==========")
    batch_size = args["batch_size"]
    lr = args["learning_rate"]
    momentum = args["momentum"]
    epochs = args["train_epochs"]
    train_split = args["split_train"]

    loader = processData(args, stageFor="train", indices=labeled)

    net = NeuralNet()
    net = net.to(device=device)

    criterion = torch.nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=float(lr), momentum=momentum)

    if resume_from is not None:
        ckpt = torch.load(os.path.join(args["EXPT_DIR"], resume_from + ".pth"))
        net.load_state_dict(ckpt["model"])
        optimizer.load_state_dict(ckpt["optimizer"])
    else:
        getdatasetstate(args)

    net.train()

    for epoch in tqdm(range(args["train_epochs"]), desc="Training"):

        running_loss = 0

        for i, batch in enumerate(loader, start=0):
            data, labels = batch

            data = data.to(device)
            labels = labels.to(device)

            optimizer.zero_grad()
            output = net(data)
            loss = criterion(output, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

            if i % 1000:
                print(
                    "epoch: {} batch: {} running-loss: {}".format(
                        epoch + 1, i + 1, running_loss / 1000
                    ),
                    end="\r",
                )
                running_loss = 0

    print("Finished Training. Saving the model as {}".format(ckpt_file))

    ckpt = {"model": net.state_dict(), "optimizer": optimizer.state_dict()}
    torch.save(ckpt, os.path.join(args["EXPT_DIR"], ckpt_file + ".pth"))

    return
Example #3
0
def train(cpu, args):
    rank = args.nr * args.cpus + cpu
    dist.init_process_group(
        backend="gloo",
        init_method="file:///C:/Users/Tung/Desktop/HK201/CN/Extra/setup.txt",
        world_size=args.world_size,
        rank=rank)
    torch.manual_seed(0)

    # Hyperparameters:
    batch_size = 100  # NOTE: If ran out of memory, try changing this value to 64 or 32
    learning_rate = 0.0001

    # Create model:
    model = NeuralNet()
    # Define loss function and optimizer:
    loss_fn = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.parameters(), learning_rate)
    # Wrap the model for ddp:
    model = nn.parallel.DistributedDataParallel(model, device_ids=None)
    # Data loading:
    train_dataset = torchvision.datasets.MNIST(root='./data',
                                               train=True,
                                               transform=transforms.ToTensor(),
                                               download=True)

    # Sampling the dataset to avoid same inputs order:
    train_sampler = torch.utils.data.distributed.DistributedSampler(
        train_dataset, num_replicas=args.world_size, rank=rank)
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               shuffle=False,
                                               num_workers=0,
                                               pin_memory=True,
                                               sampler=train_sampler)

    start = datetime.now()
    total_step = len(train_loader)
    for epoch in range(args.epochs):
        for i, (images, labels) in enumerate(train_loader):
            # Forward pass:
            outputs = model(images)
            loss = loss_fn(outputs, labels)

            # Backward pass:
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            # For logging:
            if (i + 1) % batch_size and cpu == 0:
                print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format\
                    (epoch + 1, args.epochs, i + 1, total_step,loss.item()))

    if cpu == 0:
        print("Training completed in: " + str(datetime.now() - start))
Example #4
0
def sms_reply():
    with open('dis.json', 'r') as f:
        intents = json.load(f)
    if torch.cuda.is_available():
        map_location = lambda storage, loc: storage.cuda()
    else:
        map_location = 'cpu'

    FILE = "data.pth"
    data = torch.load(FILE, map_location=map_location)

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

    model = NeuralNet(input_size, hidden_size, output_size)
    model.load_state_dict(model_state)
    model.eval()
    while True:
        # Fetch the message
        msg = request.form.get('Body')
        #sentence = input(msg)
        #if sentence == "quit":
        #    break

        #sentence = tokenize(sentence)
        msg = tokenize(msg)
        X = bag_of_words(msg, 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()]

        probs = torch.softmax(output, dim=1)
        prob = probs[0][predicted.item()]

        if prob.item() > 0.75:
            for intent in intents["intents"]:
                if tag == intent["tag"]:
                    # Create reply
                    resp = MessagingResponse()
                    resp.message(
                        random.choice(intent['responses']).format(msg))

        else:
            resp = MessagingResponse()
            resp.message("I do not understand...".format(msg))

        return str(resp)
Example #5
0
def input_process(user_input):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    with open('intents.json', 'r') as json_data:
        intents = json.load(json_data)

    FILE = "data.pth"
    data = torch.load(FILE)

    input_size = data["input_size"]
    hidden_size = data["hidden_size"]
    output_size = data["output_size"]
    all_words = data['all_words']
    tags = data['tags']
    model_state = data["model_state"]

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

    #bot_name = "Ash"
    print("Let's chat! (type 'quit' to exit)")
    while True:
        #sentence = input("You: ")
        sentence = user_input
        #if sentence == "quit":
        #break

        sentence = tokenize(sentence)
        print(sentence)
        print(all_words)
        X = bag_of_words(sentence, all_words)
        print(X)
        X = X.reshape(1, X.shape[0])
        print(X)
        X = torch.from_numpy(X).to(device)

        output = model(X)
        _, predicted = torch.max(output, dim=1)

        tag = tags[predicted.item()]

        probs = torch.softmax(output, dim=1)
        prob = probs[0][predicted.item()]
        if prob.item() > 0.75:
            for intent in intents['intents']:
                if tag == intent["tag"]:
                    #print(f"{bot_name}: {random.choice(intent['responses'])}")
                    return random.choice(intent['responses'])
        else:
            #print(f"{bot_name}: I do not understand...")
            return "I do not understand..."
Example #6
0
 def test_model_vars_after_run(self):
     args = parser.parse_args()
     json_path = os.path.join(args.params_dir, 'image_segmentation_params.json')
     assert os.path.isfile(json_path), "No json configuration file found at {}".format(json_path)
     dataset_params = param_manager.DatasetParams()
     dataset_params.update(json_path)
     dataset_dict = dataset_params.dict
     dataset = parse_image_seg.Dataset(dataset_dict)
     checkpoint_path = "./results/unitest2.ckpt"
     model = NeuralNet(dataset, self.logger, self.params)
     with model:
         model.build_model()
         model.train_model()
         self.compare_to_ckpt(model, checkpoint_path)
def get_prediction(text, embedding_matrix):

    model = NeuralNet(embedding_matrix)
    model.to(torch.device("cpu"))
    model.load_state_dict(torch.load('./pytorch_model/best_model.pt'))
    model.eval()

    with torch.no_grad():
        pred = model(text)
        pred_logit = sigmoid(pred.detach().cpu().numpy())[0][0]
        pred_label = np.where(pred_logit >= 0.5, 1, 0)

    answer = '긍정' if pred_label == 1 else '부정'
    return answer
Example #8
0
def load():
    global all_words, ids, model, nodes
    try:
        nodes = retrieve('nodes')

        data = torch.load("data.pth")

        all_words = data['all_words']
        ids = data['ids']

        model = NeuralNet(data["input_size"], data["hidden_size"], data["output_size"]).to(device)
        model.load_state_dict(data["model_state"])
        model.eval()
    except:
        print("An exception occurred")
Example #9
0
def main():
    # check if CUDA is available
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    # create DataLoader
    train_loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True, num_workers=0)

    # create model and push it to device if available
    model = NeuralNet(input_size, hidden_size, output_size).to(device)

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

    return device, train_loader, model, criterion, optimizer
Example #10
0
def solve(msg):
    with open('intents.json', 'r', encoding='utf-8', errors='ignore') as f:
        intents = json.load(f)

    FILE = "data.pth"
    data = torch.load(FILE)

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

    model = NeuralNet(input_size, hidden_size, output_size).to(device)
    model.load_state_dict(model_state)
    model.eval()
    global btags
    global bques
    sentence = msg
    sentence = tokenize(sentence)
    X = bag_of_words(sentence, all_words)
    X = X.reshape(1, X.shape[0])
    X = torch.from_numpy(X).to(dtype=torch.float).to(device)
    output = model(X)
    _, predicted = torch.max(output, dim=1)
    tag = tags[predicted.item()]
    probs = torch.softmax(output, dim=1)
    prob = probs[0][predicted.item()]
    if prob.item() > 0.75:
        for intent in intents["intents"]:
            if tag == intent["tag"]:
                if tag not in btags:
                    btags += tag + " - "
                    response = random.choice(intent['responses'])
                    return (response) + "\n\n"
                else:
                    return ""
            else:
                continue
    else:
        if msg not in bques:
            bques += msg + " - "
            global bnon
            bnon = True
            return msg + ": Mình tạm thời chưa có đáp án \n Bạn hãy để lại sđt và câu hỏi tại mục report hoặc liên hệ trực tiếp số điện thoại 0868355415" + "\n\n"
        else:
            return ""
Example #11
0
def test(args, ckpt_file):
    print("========== In the test step ==========")
    batch_size = args["batch_size"]
    lr = args["learning_rate"]
    momentum = args["momentum"]
    epochs = args["train_epochs"]
    train_split = args["split_train"]

    loader = processData(args, stageFor="test")

    net = NeuralNet()
    net = net.to(device=device)

    net.load_state_dict(
        torch.load(os.path.join(args["EXPT_DIR"], ckpt_file + ".pth"))["model"]
    )

    net.eval()
    predix = 0
    predictions = {}
    truelabels = {}

    n_val = args["test_size"]
    with tqdm(total=n_val, desc="Testing round", unit="batch", leave=False) as pbar:
        for step, (batch_x, batch_y) in enumerate(loader):
            with torch.no_grad():
                batch_x = batch_x.to(device)
                batch_y = batch_y.to(device)

                prediction = net(batch_x)

            for logit, label in zip(prediction, batch_y):
                # predictions[predix] = logit.cpu().numpy().tolist()
                truelabels[predix] = label.cpu().numpy().tolist()

                class_probabilities = logit.cpu().numpy().tolist()
                index_max = np.argmax(class_probabilities)
                predictions[predix] = index_max

                predix += 1

            pbar.update()

    # unpack predictions
    predictions = [val for key, val in predictions.items()]
    truelabels = [val for key, val in truelabels.items()]

    return {"predictions": predictions, "labels": truelabels}
def get_match_probabilities(match_fixtures):
    feature_vectors = []

    net = NeuralNet()

    for fixture in tqdm(match_fixtures, desc='Getting match probabilities...'):
        home_team, away_team = fixture['home team'], fixture['away team']
        feature_vectors.append(
            np.hstack((PREDICTED_LINEUPS[home_team],
                       PREDICTED_LINEUPS[away_team])).reshape((1, 36)))

    predictions = net.predict(np.vstack((x for x in feature_vectors)))

    match_probabilities = [x for x in predictions]

    return match_probabilities
Example #13
0
def chatbot(question):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    with open('intents.json', 'r',encoding="utf8") as json_data:
        intents = json.load(json_data)

    FILE = "data.pth"
    data = torch.load(FILE,map_location='cpu')

    input_size = data["input_size"]
    hidden_size00 = data["hidden_size00"]
    hidden_size01 = data["hidden_size01"]
    hidden_size02 = data["hidden_size02"]
    output_size = data["output_size"]
    all_words = data['all_words']
    tags = data['tags']
    model_state = data["model_state"]

    model = NeuralNet(input_size, hidden_size00, hidden_size01, hidden_size02, output_size).to(device)
    model.load_state_dict(model_state)
    model.eval()
    
    while True:
        # sentence = "do you use credit cards?"
        sentence = question
        sentence = tokenize(sentence)
        X = bag_of_words(sentence, all_words)
        X = X.reshape(1, X.shape[0])
        X = torch.from_numpy(X).to(device)

        output = model(X)
        _, predicted = torch.max(output, dim=1)

        tag = tags[predicted.item()]

        probs = torch.softmax(output, dim=1)
        prob = probs[0][predicted.item()]
        if prob.item() > 0.75:
            for intent in intents['intents']:
                if tag == intent["tag"]:
                    return(random.choice(intent['responses']))
        else:
            return('Sorry I do not understand..')
Example #14
0
def infer(args, unlabeled, ckpt_file):
    print("========== In the inference step ==========")
    batch_size = args["batch_size"]
    lr = args["learning_rate"]
    momentum = args["momentum"]
    epochs = args["train_epochs"]
    train_split = args["split_train"]

    loader = processData(args, stageFor="infer", indices=unlabeled)

    net = NeuralNet()
    net = net.to(device=device)

    net.load_state_dict(
        torch.load(os.path.join(args["EXPT_DIR"], ckpt_file + ".pth"))["model"]
    )

    net.eval()

    n_val = len(unlabeled)
    predictions = {}
    predix = 0

    with tqdm(total=n_val, desc="Inference round", unit="batch", leave=False) as pbar:
        for step, (batch_x, batch_y) in enumerate(loader):
            with torch.no_grad():
                batch_x = batch_x.to(device)
                batch_y = batch_y.to(device)
                prediction = net(batch_x)

            for logit in prediction:
                predictions[unlabeled[predix]] = {}

                class_probabilities = logit.cpu().numpy().tolist()
                predictions[unlabeled[predix]]["pre_softmax"] = class_probabilities
                index_max = np.argmax(class_probabilities)
                predictions[unlabeled[predix]]["prediction"] = index_max
                predix += 1

            pbar.update()

    return {"outputs": predictions}
Example #15
0
def load_chatbot():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') #if we have GPU support 

    with open('CB.json','r') as f:
        intents = json.load(f)

    FILE="data.pth"
    data = torch.load(FILE)
    input_size = data["input_size"]
    hidden_size = data["hidden_size"]
    output_size = data["output_size"]
    all_words = data["all_words"]
    tags = data["tags"]
    model_state = data["model_state"]


    model = NeuralNet(input_size, hidden_size, output_size)
    model.load_state_dict(model_state)
    model.eval()
    return model, all_words, tags, intents
Example #16
0
def chatbot():
    device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    with open('intents.json','r') as f:
        intents=json.load(f)        
    FILE="data.pth"
    data=torch.load(FILE)
    input_size = data["input_size"]
    hidden_size = data["hidden_size"]
    output_size = data["output_size"]
    all_words = data['all_words']
    tags = data['tags']
    model_state = data["model_state"]

    model = NeuralNet(input_size, hidden_size, output_size).to(device)
    model.load_state_dict(model_state)
    model.eval()
   
    sentence = request.args.get("msg") #get data from input,we write js  to index.html
    sentence=tokenize(sentence)
    X=bag_of_words(sentence,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()]
    bot="I do not know...try something different😊"
    probs = torch.softmax(output, dim=1)
    prob = probs[0][predicted.item()]
    print(prob.item())
    if prob.item() < 0.75:
        bot="I do not know...try something different😊"
        
    
    elif prob.item() > 0.75:
        for intent in intents['intents']:
            if tag == intent["tag"]:
                bot=random.choice(intent['responses'])      
        
        
    return bot
Example #17
0
def process(s):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    with open('intents.json', 'r') as f:
        intents = json.load(f)

    FILE = "data.pth"
    data = torch.load(FILE)

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

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

    bot_name = "Zoey"

    sentance = tokenize(s)
    x = bag_of_words(sentance, all_words)
    x = x.reshape(1, x.shape[0])
    x = torch.from_numpy(x)
    output = model(x)
    _, predicted = torch.max(output, dim=1)
    probability = torch.softmax(output, dim=1)
    probability = probability[0][predicted.item()]
    if (probability < .45):
        return "sorry"
    tag = tags[predicted.item()]
    return tag


#	for intent in intents["intents"]:
#		if tag == intent["tag"]:
#			choice = random.choice(intent["responses"])
#			print(f"{bot_name}: {choice}")
Example #18
0
def main():
    max_features = 21128
    maxlen = 50
    BATCH_SIZE = 128
    INPUT_DIM = max_features
    EMB_DIM = 128
    HID_DIM_1 = 60
    HID_DIM_2 = 16
    OUTPUT_DIM = 3
    h_data, text_data, y_data = load_smart_eyes_data()
    logger.info('text train shape: {}'.format(text_data.shape))
    x_train, x_val, y_train, y_val = train_test_split(text_data,
                                                      y_data,
                                                      test_size=0.2,
                                                      random_state=42)
    model = NeuralNet(INPUT_DIM, EMB_DIM, HID_DIM_1, HID_DIM_2, OUTPUT_DIM,
                      maxlen)
    optimizer = torch.optim.Adam(model.parameters())

    trainer = Trainer(batch_size=BATCH_SIZE,
                      model=model,
                      optimizer=optimizer,
                      epochs=30)
    trainer.fit(x_train, y_train, x_val, y_val)
Example #19
0
# hyperparameters
batch_size = 8
hidden_size = 8
output_size = len(tags)
input_size = len(X_train[0])
learning_rate = 0.001
num_epochs = 1000

dataset = ChatDataset()
train_loader = DataLoader(dataset=dataset,
                          batch_size=batch_size,
                          shuffle=True,
                          num_workers=0)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = NeuralNet(input_size, hidden_size, output_size).to(device)

#loss and optimizer
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)

        #forward
        outputs = model(words)
        loss = criterion(outputs, labels)

        # backward and optimizer
Example #20
0
    intents = json.load(f)

# Load data
FILE = "data.pth"
data = torch.load(FILE)

# Extract data to variables
input_size = data["input_size"]
hidden_size = data["hidden_size"]
output_size = data["output_size"]
all_words = data["all_words"]
tags = data["tags"]
model_state = data["model_state"]

# Load model parameters
model = NeuralNet(input_size, hidden_size, output_size)
model.load_state_dict(model_state)
model.eval()

# Create bot
# Define a bot name
bot_name = "Atlas"
# Welcome message and exit instruction
print("Welcome here! Type 'quit' to exit.")
while True:
    # Get input from user
    sentence = input("You: ")
    # Exit program if quit entered
    if sentence == "quit":
        break
Example #21
0
import streamlit as st
from numpy import genfromtxt
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
from model import NeuralNet
from dataset import inference_picture, data_to_picture

net = NeuralNet(1296, random_state=2020)
net.load_weights()
# noinspection SpellCheckingInspection
st.set_option('deprecation.showfileUploaderEncoding', False)


def image_preprocessing(data):
    """
    Prepare data to inference
    :param data: Data in numpy array
    :return: Numpy array
    """
    inference_path = data_to_picture(data)
    inference_png = inference_picture(inference_path)
    return inference_png


def inference(model, image):
    """
    Make inference
    :param model: NeuralNet
    :param image: Data to process
    :return: probability for classes and class prediction
Example #22
0
    attributes = json.load(f)

# Load the creadential saved during training

FILE = "data.pth"
data = torch.load(FILE)

# load all the saved values
inputSize = data["input_size"]
outputSize = data["output_size"]
hiddenSize = data["hidden_size"]
allWords = data["allWords_size"]
tags = data["tags"]
modelState = data["modelState"]

model = NeuralNet(inputSize, hiddenSize, outputSize)
# synthesize next Html tag or attribute provided


def synthesizeTag(sentence, botName, recType):
    # load the statedictionary
    model.load_state_dict(modelState)
    model.eval()

    # tokenize find BOG predict the class for new sentence
    x = tokenizeAndStemSpoken(sentence, allWords)

    # find the predicted output
    output = model(x)
    _, predicted = torch.max(output, dim=1)
    tag = tags[predicted.item()]
Example #23
0
dataset = ChatDataset()

# Set model in place
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

train_loader = DataLoader(dataset=dataset,
                          batch_size=batch_size,
                          shuffle=True,
                          num_workers=0)  #2 if everything is fine else 0

i_size = len(X_train[0])
h_size = 8
o_size = len(tags)

model = NeuralNet(i_size, h_size, o_size).to(device)

#Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

n_epoch = 1000

for epoch in range(n_epoch):
    for words, labels in train_loader:
        words = words.to(device)
        labels = labels.to(device)

        outputs = model(words)
        #labels = torch.max(labels, 1)[1]
        loss = criterion(outputs, labels.long())
Example #24
0
import torch
import torch.nn as nn
from torch import optim
from sklearn.datasets import load_digits

from random import randint

from model import NeuralNet, loss_fn, device

digits = load_digits()

X = torch.tensor(digits['data'], dtype=torch.float32).to(device)
Y = torch.tensor(digits['target'], dtype=torch.int64).to(device)

model = NeuralNet()
optimizer = optim.Adam(model.parameters())

i = 100
for epoch in range(i):
    optimizer.zero_grad()
    y_predict = model(X)
    loss = loss_fn(y_predict, Y)
    loss.backward()
    optimizer.step()

    if epoch % 10 == 0:
        print('Epoch {:4d}/{} Cost: {:.6f}'.format(epoch, i, loss.item()))
Example #25
0
    #             Q_targets.append(labels.numpy())
    #     Q_preds = np.array(Q_preds)
    #     Q_targets = np.array(Q_targets)
    #     Q_preds = Q_preds.reshape((-1, 1))
    #     Q_targets = Q_targets.reshape((-1, 1))
    #     print(cv_rmse(torch.from_numpy(Q_preds), torch.from_numpy(Q_targets)))
    #     print(Q_preds[:24])
    #     print(Q_targets[:24])


if __name__ == "__main__":
    # daily_model = concatNN(20, 256, 3, 1, "add")
    # daily_model.load_state_dict(torch.load('dailynn_W.pth'))
    # hour_model = HourRNN(5, 256, 3, 1)
    # # hour_model = HourNN()
    # hour_model.load_state_dict(torch.load('hournn_W.pth'))
    # test(daily_model, hour_model, "W")

    daily_model = NeuralNet(5, 256, 1)
    daily_model.load_state_dict(torch.load('dailynn_Q.pth'))

    hour_model = HourRNN(5, 256, 3, 1)
    hour_model.load_state_dict(torch.load('hournn.pth'))
    test(daily_model, hour_model, "Q", "regression", 2, 2017)

    # daily_model = CNN(1)
    # daily_model.load_state_dict(torch.load('dailynn_q_CNN.pth'))
    # hour_model = HourRNN(5, 256, 3, 1)
    # hour_model.load_state_dict(torch.load('hournn.pth'))
    # test(daily_model, hour_model, "Q","time_series",  2, 2017)
Example #26
0
 def __init__(self, board, piece, LR, train):
     super().__init__(board, piece, LR, train)
     self.model = NeuralNet()
     self.optimizer = th.optim.Adam(self.model.parameters(), lr=LR)
     self.loss_func = th.nn.MSELoss()
Example #27
0
    def train(self):
        # create_training_data first
        self.create_training_data()

        # Hyper-parameters
        num_epochs = 800
        batch_size = 8
        learning_rate = 0.001
        input_size = len(self.x_train[0])
        hidden_size = 8
        output_size = len(self.tags)

        dataset = IntentDataset(self.x_train, self.y_train)
        train_loader = DataLoader(dataset=dataset,
                                  batch_size=batch_size,
                                  shuffle=True,
                                  num_workers=2)
        # if using Python3.8, set num_workers=0. Python3.8 has a spawn vs
        # fork issue that causes this to fail if num_workers > 0

        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

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

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

        # Train the model
        for epoch in range(num_epochs):
            for (words, labels) in train_loader:
                words = words.to(device)
                labels = labels.to(device)

                # Forward pass
                outputs = model(words)
                # if y would be one-hot, we must apply
                # labels = torch.max(labels, 1)[1]
                loss = criterion(outputs, labels)

                # Backwards and optimize
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

            if (epoch + 1) % 100 == 0:
                print(
                    f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

        print(f'final loss: {loss.item():.4f}')

        data = {
            "model_state": model.state_dict(),
            "input_size": input_size,
            "hidden_size": hidden_size,
            "output_size": output_size,
            "all_words": self.all_words,
            "tags": self.tags
        }

        FILE = bumblebee_root + "models/" + self.model_name + ".pth"
        torch.save(data, FILE)

        print(f'training complete. file saved to {FILE}')
Example #28
0
from utils import Dataset
from consts import GAME_LENGTH

EPOCHS = 4


dataset = Dataset('experience-goat.txt')
dataloader = th.utils.data.DataLoader(dataset, batch_size=GAME_LENGTH)


device = th.device('cuda:0' if th.cuda.is_available() else 'cpu')

# LRs = [0.001, 0.01, 0.0025]
LRs = [0.0025]
for LR in LRs:
    model = NeuralNet().to(device)
    # model = th.load('tigerModel-learn.pt')
    optimizer = th.optim.Adam(model.parameters(), lr=LR)
    loss_func = th.nn.MSELoss()

    avg_loss = []
    for _ in tqdm(range(EPOCHS)):
        total_loss = 0
        for inp, target in tqdm(dataloader, leave=False):
            # print(inp)
            # print(type(th.tensor(inp)))
            # break
            optimizer.zero_grad()

            inp = inp.float().to(device)
            pred = model.layers(inp)
Example #29
0
def train():
    nodes = retrieve('nodes')

    all_words = []
    ids = []
    xy = []
    # loop through each sentence in our node patterns
    for node in nodes:
        # add to id list
        ids.append(node['id'])
        for pattern in node['patterns']:
            # tokenize each word in the sentence
            w = tokenize(pattern)
            # add to our words list
            all_words.extend(w)
            # add to xy pair
            xy.append((w, node['id']))

    # stem and lower each word and remove stop words
    ignore_words = ['?', '.', '!', '(', ')']
    stop_words = retrieve('stop_words')
    all_words = [w for w in all_words if not w.lower() in stop_words]
    all_words = [stem(w) for w in all_words if w not in ignore_words]

    # remove duplicates and sort
    all_words = sorted(set(all_words))
    ids = sorted(set(ids))

    # create training data
    x_train = []
    y_train = []
    for (pattern_sentence, id) in xy:
        # X: bag of words for each pattern_sentence
        bag = bag_of_words(pattern_sentence, all_words)
        x_train.append(bag)

        # y: PyTorch CrossEntropyLoss needs only class labels, not one-hot
        y_train.append(ids.index(id))

    x_train = np.array(x_train)
    y_train = np.array(y_train)

    # Hyper-parameters
    num_epochs = 1000
    batch_size = 8
    learning_rate = 0.001
    input_size = len(x_train[0])
    hidden_size = 8
    output_size = len(ids)

    class ChatDataset(Dataset):
        def __init__(self):
            self.n_samples = len(x_train)
            self.x_data = x_train
            self.y_data = y_train

        # support indexing such that dataset[i] can be used to get i-th sample
        def __getitem__(self, index):
            return self.x_data[index], self.y_data[index]

        # we can call len(dataset) to return the size
        def __len__(self):
            return self.n_samples

    dataset = ChatDataset()
    train_loader = DataLoader(dataset=dataset,
                              batch_size=batch_size,
                              shuffle=True,
                              num_workers=0)

    device = torch.device('cpu')

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

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

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

            # Forward pass
            outputs = model(words)
            # if y would be one-hot, we must apply
            # labels = torch.max(labels, 1)[1]
            loss = criterion(outputs, labels)

            # Backward and optimize
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

    data = {
        "model_state": model.state_dict(),
        "input_size": input_size,
        "hidden_size": hidden_size,
        "output_size": output_size,
        "all_words": all_words,
        "ids": ids
    }

    torch.save(data, "data.pth")
Example #30
0
def train(num_epochs=500, learning_rate=0.001):
    global intents
    # Use of a JSON-File to read trough the training data
    with open("intents.json", "r", encoding="UTF-8") as f:
        intents = json.load(f)

    # Will hold every word to tokenize and stem them
    all_words = []

    # Will hold every tag to classify the words
    tags = []

    # Will hold patterns and tags
    xy = []

    # the JSON-file is treated like a dictionary, therefore we have to use a key for the loop
    for intent in intents["intents"]:
        tag = intent["tag"]
        tags.append(tag)
        for pattern in intent["patterns"]:
            w = tokenize(pattern)
            # We don´t want to have lists in the all_words list, therefore we extend instead of appending them
            all_words.extend(w)
            # to be able to link the words to the different tags
            xy.append((w, tag))

    # setting up the excluded characters
    ignore_words = ["?", "!", ".", ","]
    all_words = [stem(w) for w in all_words if w not in ignore_words]

    # getting a alphabetically sorted list without duplicate words (function of set)
    all_words = sorted(set(all_words))
    tags = sorted(set(tags))

    X_train = []
    Y_train = []

    for pattern_sentence, tag in xy:
        bag = bag_of_words(pattern_sentence, all_words)
        X_train.append(bag)

        # Get the index of the tag of the tags-list
        label = tags.index(tag)
        Y_train.append(label)  # CrossEntropyLoss

    # Create np.arrays, arrays with only zeros with the length of corresponding data
    X_train = np.array(X_train)
    Y_train = np.array(Y_train)

    # Dataset-Class to train it easily
    class ChatDataSet(Dataset):
        def __init__(self):
            self.n_samples = len(X_train)
            self.x_data = X_train
            self.y_data = Y_train

        def __getitem__(self, index):
            return self.x_data[index], self.y_data[index]

        def __len__(self):
            return self.n_samples

    # Hyperparameters
    batch_size = 8
    hidden_size = 80
    output_size = len(tags)
    input_size = len(all_words)

    # Creating a custom data-set to feed into the neural network
    dataset = ChatDataSet()
    train_loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True)

    # Checking if working with gpu is available
    device = torch.device("cpu")

    # Defining the model and using it for training
    model = NeuralNet(input_size, hidden_size, output_size).to(device)

    # loss and optimizer
    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(device, torch.int64)

            # forward
            outputs = model(words)
            loss = criterion(outputs, labels)

            # backward and optimizer step
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        if epoch % 100 == 0:
            print("Epoch " + str(epoch) + " finished! " + f"loss={loss.item():.4}" + "\n " + str(num_epochs - epoch)
                  + " remaining!")

    data = {
        "model_state": model.state_dict(),
        "input_size": input_size,
        "output_size": output_size,
        "hidden_size": hidden_size,
        "all_words": all_words,
        "tags": tags
    }

    FILE = "Terra-Speak.pth"
    torch.save(data, FILE)

    print(f"Training complete! Model named {FILE} saved.")