Exemplo n.º 1
0
def testRun():
    # MNIST dataset
    train_dataset = torchvision.datasets.MNIST(root='/data',
                                               train=True,
                                               transform=transforms.ToTensor(),
                                               download=True)

    cv_splits = 3
    kfold = KFold(n_splits=cv_splits, shuffle=True, random_state=0)
    for fold_idx, (train_idx,
                   valid_idx) in enumerate(kfold.split(train_dataset)):
        print(">> CV fold step ", str(fold_idx))
        # cnn model
        model = mlp.SimpleMLP(output_classes).to(device)
        # Loss and optimizer
        criterion = nn.CrossEntropyLoss()
        optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

        # Data loader
        train_loader = DataLoader(Subset(train_dataset, train_idx),
                                  batch_size=batch_size,
                                  shuffle=True)

        # Training
        mnist_trainer = trainer.MLPTrainer(train_loader,
                                           model=model,
                                           cri=criterion,
                                           opt=optimizer,
                                           device=device)
        train_result = mnist_trainer.Execute(epochs)
Exemplo n.º 2
0
def mlp(X_train, y_train, X_test, y_test):
    # mlp
    mlp = MLP(X_train, y_train, X_test, y_test)
    # mlp train
    mlp.mlp_train()
    # save model
    # predict
    mlp_predict = mlp.mlp_predict_classes().reshape(len(X_test), )
    # predict probability
    mlp_predict_prob = mlp.mlp_predict_prob()
    mlp_predict_prob = np.hstack((1 - mlp_predict_prob, mlp_predict_prob))
    return [mlp, mlp_predict + 1, mlp_predict_prob]
Exemplo n.º 3
0
 def run(self):  # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
     if self.model_map[self.threadID] == "Rule_based":
         # 等待Hello
         protobufhello = self.tcpCliSock.recv(BUFSIZ)
         hello_message = message.Hello()
         hello_message.ParseFromString(protobufhello)
         print(hello_message)
         print("Hello")
         hello_message.code = 1 # Rule_based
         self.tcpCliSock.send(hello_message.SerializeToString())
         while True:
             print("is recving")
             protobufdata = self.tcpCliSock.recv(BUFSIZ)
             print("recv")
             game_state_message = message.GameState()  # 读取GameState
             game_state_message.ParseFromString(protobufdata)
             player = game_state_message.who
             if player == 0:
                 # Declare
                 card = declarer(game_state_message, "hard")
                 # 发送
             elif player == 1:
                 card = lopp(game_state_message, "hard")
             elif player == 2:
                 card = dummy(game_state_message, "hard")
             else:
                 card = ropp(game_state_message, "hard")
             play = message.Play()
             play.who = player
             play.card.CopyFrom(card)
             self.tcpCliSock.send(play.SerializeToString())
             print("play")
     elif self.model_map[self.threadID] == "DQN_SL":
         device = torch.device('cpu')
         net = DQN().to(device)
         net.eval()
         net.load_state_dict(torch.load('models/trained_agents/bridge_agent_14-16_v5.pth', map_location=torch.device('cpu')))
         protobufhello = self.tcpCliSock.recv(BUFSIZ)
         hello_message = message.Hello()
         hello_message.ParseFromString(protobufhello)
         print(hello_message)
         print("Hello")
         hello_message.code = 2
         self.tcpCliSock.send(hello_message.SerializeToString())
         while True:
             print("is recving")
             protobufdata = self.tcpCliSock.recv(BUFSIZ)
             print("recv")
             game_state_message = message.GameState()  # 读取GameState
             game_state_message.ParseFromString(protobufdata)
             player = game_state_message.who
             feature = GameState2feature_DQN(game_state_message)
             card_logits = net(torch.tensor(feature).float())
             # 加一层mask
             validPlays = game_state_message.validPlays
             output_mask = [0] * 52
             for card in validPlays:
                 output_mask[card.suit * 13 + card.rank] = 1
             masked_logits = card_logits.data * torch.tensor(output_mask) + torch.tensor(output_mask)
             pred = masked_logits.max(0)[1]
             card = message.Card()
             card.suit = int(int(pred) / 13)
             card.rank = int(pred) % 13
             play = message.Play()
             play.who = player
             play.card.CopyFrom(card)
             self.tcpCliSock.send(play.SerializeToString())
             print("play")
     elif self.model_map[self.threadID] == "DQN_RL":
         device = torch.device('cpu')
         net = [DQN().to(device) for i in range(4)]
         map_dirname = {0:'declarer',1:'lopp',2:'dummy',3:'ropp'}
         for i in range(4):
             net[i].eval()
             net[i].load_state_dict(torch.load('models/DQN/'+map_dirname[i]+'/policy-network-30000.pth', map_location=torch.device('cpu')))
         protobufhello = self.tcpCliSock.recv(BUFSIZ)
         hello_message = message.Hello()
         hello_message.ParseFromString(protobufhello)
         print(hello_message)
         print("Hello")
         hello_message.code = 3
         self.tcpCliSock.send(hello_message.SerializeToString())
         while True:
             print("is recving")
             protobufdata = self.tcpCliSock.recv(BUFSIZ)
             print("recv")
             game_state_message = message.GameState()  # 读取GameState
             game_state_message.ParseFromString(protobufdata)
             player = game_state_message.who
             feature = GameState2feature_DQN(game_state_message)
             card_logits = net[player](torch.tensor(feature).float())
             # 加一层mask
             validPlays = game_state_message.validPlays
             output_mask = [0] * 52
             for card in validPlays:
                 output_mask[card.suit * 13 + card.rank] = 1
             masked_logits = card_logits.data * torch.tensor(output_mask) + torch.tensor(output_mask)
             pred = masked_logits.max(0)[1]
             card = message.Card()
             card.suit = int(int(pred) / 13)
             card.rank = int(pred) % 13
             play = message.Play()
             play.who = player
             play.card.CopyFrom(card)
             self.tcpCliSock.send(play.SerializeToString())
             print("play")
     elif self.model_map[self.threadID] == "MLP":
         device = torch.device('cpu')
         net = MLP().to(device)
         net.load_state_dict(torch.load('models/SL_V1.pth', map_location=torch.device('cpu')))
         protobufhello = self.tcpCliSock.recv(BUFSIZ)
         hello_message = message.Hello()
         hello_message.ParseFromString(protobufhello)
         print(hello_message)
         print("Hello")
         hello_message.code = 2
         self.tcpCliSock.send(hello_message.SerializeToString())
         while True:
             print("is recving")
             protobufdata = self.tcpCliSock.recv(BUFSIZ)
             print("recv")
             game_state_message = message.GameState()  # 读取GameState
             game_state_message.ParseFromString(protobufdata)
             player = game_state_message.who
             feature = GameState2feature_MLP(game_state_message)
             card_logits = net(torch.tensor(feature).float())
             # 加一层mask
             validPlays = game_state_message.validPlays
             output_mask = [0] * 52
             for card in validPlays:
                 output_mask[card.suit * 13 + card.rank] = 1
             masked_logits = card_logits.data * torch.tensor(output_mask) + torch.tensor(output_mask)
             pred = masked_logits.max(0)[1]
             card = message.Card()
             card.suit = int(int(pred) / 13)
             card.rank = int(pred) % 13
             play = message.Play()
             play.who = player
             play.card.CopyFrom(card)
             self.tcpCliSock.send(play.SerializeToString())
             print("play")
     elif self.model_map[self.threadID] == "CNN_SL":
         device = torch.device('cpu')
         net = ConvFCNet().to(device)
         model = torch.load('models/cnn_model.pkl',map_location=torch.device('cpu'))
         if isinstance(model, torch.nn.DataParallel):
             net = model.module
         net.eval()
         protobufhello = self.tcpCliSock.recv(BUFSIZ)
         hello_message = message.Hello()
         hello_message.ParseFromString(protobufhello)
         print(hello_message)
         print("Hello")
         hello_message.code = 2
         self.tcpCliSock.send(hello_message.SerializeToString())
         while True:
             print("is recving")
             protobufdata = self.tcpCliSock.recv(BUFSIZ)
             print("recv")
             game_state_message = message.GameState()  # 读取GameState
             game_state_message.ParseFromString(protobufdata)
             player = game_state_message.who
             feature = GameState2feature_cnn(game_state_message)
             card_logits = net(torch.tensor(feature).float()[None,:])
             resort_card_logits = torch.zeros_like(card_logits[0])
             resort_card_logits[:13] = card_logits[0,-13:]
             resort_card_logits[13:26] = card_logits[0,-26:-13]
             resort_card_logits[26:39] = card_logits[0,13:26]
             resort_card_logits[-13:] = card_logits[0,0:13]
             # 加一层mask
             validPlays = game_state_message.validPlays
             output_mask = [0] * 52
             for card in validPlays:
                 output_mask[card.suit * 13 + card.rank] = 1
             masked_logits = torch.nn.functional.softmax(resort_card_logits, 0).data * torch.tensor(output_mask) + torch.tensor(output_mask)
             pred = masked_logits.max(0)[1]
             card = message.Card()
             card.suit = int(int(pred) / 13)
             card.rank = int(pred) % 13
             play = message.Play()
             play.who = player
             play.card.CopyFrom(card)
             self.tcpCliSock.send(play.SerializeToString())
             print("play")
Exemplo n.º 4
0
                               config.batch_size,
                               config.epoch,
                               padding=True,
                               sort=False)
train_sim_iter = batch_sort_iter(train_sim,
                                 config.batch_size,
                                 config.epoch,
                                 padding=False)

# large dev evaluation will result in memory issue
N = 1000
dev_q_t = to_tensor(dev_q[:N], padding=True)
dev_a_t = to_tensor(dev_a[:N], padding=True, sort=False)
dev_sim_t = torch.LongTensor(dev_sim[:N])

model = MLP(config)
optimizer = optim.SGD(model.parameters(), lr=config.lr)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
dev_q_t, dev_a_t, dev_sim_t = dev_q_t.to(device), dev_a_t.to(
    device), dev_sim_t.to(device)

#pdb.set_trace()


def train():
    cnt = 0
    pbar = tqdm(zip(train_q_iter, train_a_iter, train_sim_iter),
                total=n_batches)
    val_loss = -1
Exemplo n.º 5
0
def RunTorchCV():

    with mlflow.start_run():

        # MNIST dataset
        train_dataset = torchvision.datasets.MNIST(
            root='/data',
            train=True,
            transform=transforms.ToTensor(),
            download=True)

        train_results = {}
        valid_results = {}

        cv_splits = 3
        kfold = KFold(n_splits=cv_splits, shuffle=True, random_state=0)
        for fold_idx, (train_idx,
                       valid_idx) in enumerate(kfold.split(train_dataset)):
            print(">> CV fold step ", str(fold_idx))
            # cnn model
            model = mlp.SimpleMLP(output_classes).to(device)
            # Loss and optimizer
            criterion = nn.CrossEntropyLoss()
            optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

            # Data loader
            train_loader = DataLoader(Subset(train_dataset, train_idx),
                                      batch_size=batch_size,
                                      shuffle=True)
            valid_loader = DataLoader(Subset(train_dataset, valid_idx),
                                      batch_size=batch_size,
                                      shuffle=False)

            # Training
            mnist_trainer = trainer.MLPTrainer(train_loader,
                                               model=model,
                                               cri=criterion,
                                               opt=optimizer,
                                               device=device)
            train_result = mnist_trainer.Execute(epochs)
            trained_model = mnist_trainer.GetModel()
            train_results[fold_idx] = train_result

            # Validation
            mnist_validator = validator.MLPValidator(valid_loader,
                                                     model=trained_model,
                                                     criterion=criterion,
                                                     device=device)
            valid_result = mnist_validator.Validate()
            valid_results[fold_idx] = valid_result

        mlflow.log_param("method_name",
                         mlp.SimpleMLP(output_classes).__class__.__name__)
        mlflow.log_param("output_class", output_classes)
        mlflow.log_param("batch_size", batch_size)
        mlflow.log_param("learning_rate", learning_rate)

        mlflow.log_param("fold_type", kfold.__class__.__name__)
        mlflow.log_param("n_splits", cv_splits)
        mlflow.log_param("random_state", 0)

        mlflow.log_param("criterion", nn.CrossEntropyLoss.__class__.__name__)
        mlflow.log_param("optimizer", torch.optim.Adam.__name__)

        average_loss = 0
        average_acc = 0
        for fold_idx, cv_result in train_results.items():
            loss = cv_result[cv_splits - 1]["loss"]
            acc = cv_result[cv_splits - 1]["accuracy"]
            average_loss += loss
            average_acc += acc
            mlflow.log_metric("fold_" + str(fold_idx) + "_loss", loss)
            mlflow.log_metric("fold_" + str(fold_idx) + "_accuracy", acc)

        average_loss = average_loss / cv_splits
        average_acc = average_acc / cv_splits
        mlflow.log_metric("average_loss", average_loss)
        mlflow.log_metric("average_acc", average_acc)

    return valid_results
Exemplo n.º 6
0
#!/usr/bin/python
from __future__ import division
from config import config
from utils import *
from data_loader import *
import pdb
from Model import MLP
from tqdm import tqdm

# dump path
train_file_path = "./pkl/reader/300/train_pair.pkl"
dev_file_path = "./pkl/reader/300/dev_pair.pkl"
test_file_path = "./pkl/reader/300/test_pair.pkl"

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MLP(config)
model = model.to(device)
model.load(config.pre_embed_file)

title_dict = load_from_file(config.title_dict)
entity_dict = load_from_file(config.entity_dict)


def predict_sim(x, y):
    x = torch.LongTensor([x])
    y = torch.LongTensor([y])
    sim = torch.LongTensor([1])
    x, y, sim = x.to(device), y.to(device), sim.to(device)
    #pdb.set_trace()
    loss = model.forward(x, y, sim)
    return loss.item()
with open('letter-recognition.data', 'r') as file:
    lines = file.readlines()

data = []
for line in lines:
    values = line.split(',')
    # Divide by 15 for Min Max Normalisation
    input = [(int(x.replace('\n', '')) / 15) for x in values[1:]]
    label = charToOneHot(values[0])
    data.append([input, label])

dataSet = DataSet(data, 0.8)

# Load or insantiate new MLP
if len(sys.argv) == 1:
    mlp = MLP(dataSet.numInputs, numHidden, dataSet.numOutputs, outputActivation='SOFTMAX', hiddenActivation=hiddenActivation)
    train = True
elif sys.argv[1] == 'load':
    mlp = MLP(loadModel=True)
    train = False
    numEpochs = 1

# Train and test
for epoch in range(numEpochs):
    trainLoss, trainAccuracy = mlp.process(dataSet.trainData, train=train, learningRate=learningRate, updateFreq=updateFreq)
    if epoch % printFreq == 0 or epoch == numEpochs - 1:
        testLoss, testAccuracy = mlp.process(dataSet.testData, train=False)
        print("EPOCH:      ", epoch)
        print("TRAIN LOSS: ", trainLoss)
        print("TRAIN ACC:  ", trainAccuracy, "%")
        print("TEST LOSS:  ", testLoss)
Exemplo n.º 8
0
                    [[1, 1], [1, 0]]])

# Best learning rates for each determined through experimenting
if outputActivation == 'SIGMOID':
    dataSet = dataSet1
    learningRate = 1
if outputActivation == 'TANH':
    dataSet = dataSet2
    learningRate = 1
if outputActivation == 'RELU':
    dataSet = dataSet1
    learningRate = 0.01
if outputActivation == 'SOFTMAX':
    dataSet = dataSet3
    learningRate = 0.1
if 'dataSet' not in list(locals()) + list(globals()):
    print("\'" + outputActivation + "\' is not a valid activation function.")
    sys.exit()

mlp = MLP(dataSet.numInputs, 2, dataSet.numOutputs, outputActivation,
          hiddenActivation)

for epoch in range(numEpochs):
    epochLoss, epochAccuracy = mlp.process(dataSet.trainData,
                                           train=True,
                                           learningRate=learningRate)
    if epoch % printFreq == 0:
        print("EPOCH:      ", epoch)
        print("TRAIN LOSS: ", epochLoss)
        print("TRAIN ACC:  ", epochAccuracy, "%\n")
Exemplo n.º 9
0
#!/usr/bin/python
from __future__ import division
from config import config
from utils import *
from data_loader import *
import pdb
from Model import MLP

# dump path
train_file_path = "./pkl/reader/300/train_pair.pkl"
dev_file_path = "./pkl/reader/300/dev_pair.pkl"
test_file_path = "./pkl/reader/300/test_pair.pkl"

model = MLP(config)
model.load(config.pre_embed_file)

title_dict = load_from_file(config.title_dict)
entity_dict = load_from_file(config.entity_dict)


def predict_sim(x, y):
    x = torch.LongTensor([x])
    y = torch.LongTensor([y])
    sim = torch.LongTensor([1])
    #pdb.set_trace()
    loss = model.forward(x, y, sim)
    return loss.data.numpy()[0]


def extract_ans_pair(golds, wiki_ans):
    s1 = set(golds)