def train_model(model, x_train, y_train, num_of_epochs, batch_size,
                position_or_section):
    class_weights = None
    if position_or_section.upper() == "POSITION":
        position_or_section = "Position"
    else:
        position_or_section = "Section"
        class_weights = {0: 1., 1: 2., 2: 2., 3: 1.}
    dashes = "-" * 50
    print(dashes + " " + position_or_section + " model training " + dashes)
    ANN.train_NN(model, x_train, y_train, class_weights, num_of_epochs,
                 batch_size)
    print(dashes * 2)
Example #2
0
def main():
    config = Config.instance()
    loader = UCIStudentPerformance(batch_size=config.BATCH_SIZE,
                                   is_eval=config.TRAIN_MODE,
                                   debug=config.DEBUG_MODE)

    model = ANN(config.DEBUG_MODE)

    optimizer = torch.optim.SGD(model.parameters(), lr=config.LEARNING_RATE)
    trainer = ANNTrainer(model, loader, optimizer)
    if config.TRAIN_MODE:
        trainer.train(config.MAX_EPOCH, config.BATCH_SIZE)
    else:
        trainer.evaluate()
def do_ANN(x_train, y_train, x_test, y_test, position_or_section):
    if position_or_section.upper() == "POSITION":
        model = ANN.make_NN(x_train, 27)
    elif position_or_section.upper() == "SECTION":
        model = ANN.make_NN(x_train, 4)
    else:
        raise ValueError("Invalid value for position_or_section parameter")

    # tried number of epochs: 10, 100, 1000 - none of them gave an accuracy of over 89%
    # tried batch sizes: 10, 20, 50, 100, 200, 500 - none of them gave an accuracy of over 89%
    train_model(model, x_train, y_train, 10, 100, position_or_section)

    print("Test set evaluation: ",
          model.evaluate(x_test, y_test, batch_size=100))
    predictions = model.predict(x_test)
    def __init__(self, state_size, action_size, seed):
        """Initialize an Agent object.
        
        Params
        ======
            state_size (int): dimension of each state
            action_size (int): dimension of each action
            seed (int): random seed
        """
        self.state_size = state_size
        self.action_size = action_size
        self.seed = random.seed(seed)

        # Q-Network
        self.qnetwork_local = ANN(state_size, action_size, seed).to(device)
        self.qnetwork_target = ANN(state_size, action_size, seed).to(device)
        self.optimizer = optim.Adam(self.qnetwork_local.parameters(), lr=LR)

        # Replay memory
        self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, seed)
        # Initialize time step (for updating every UPDATE_EVERY steps)
        self.t_step = 0
Example #5
0
def train():
    # load data
    X, y = load_data(DATA_PATH, IMP_M, SCALE_M)

    # split train/valid/test
    print('\nSplit... (65% train, 20% valid, 15% test)')
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.15,
                                                        random_state=seed)
    print('Number of training+validation data:', X_train.shape[0])
    print('Number of testing data:', X_test.shape[0])

    # model
    if (not os.path.isfile(ANN_PATH)) | ANN_AGAIN:
        print('\nTraining ANN...')
        from model import ANN
        model = ANN(X_train,
                    y_train,
                    isW,
                    W_TRAINABLE,
                    ANN_PATH,
                    isPlot=OUTPUT_DIR)
    else:
        print('\nLoading ANN...')
        model = load_model(ANN_PATH)

    # evaluate model
    train_scores = model.evaluate(X_train, y_train, verbose=0)
    print("Train %s: %.2f%%" % (model.metrics_names[1], train_scores[1] * 100))
    test_scores = model.evaluate(X_test, y_test, verbose=0)
    print("Test %s: %.2f%%" % (model.metrics_names[1], test_scores[1] * 100))

    # evaluation
    if isEval:
        print('\nEvaluating...')
        from eval import eval
        eval(model, X_test, y_test, OUTPUT_DIR)
class Agent():
    """Interacts with and learns from the environment."""
    def __init__(self, state_size, action_size, seed):
        """Initialize an Agent object.
        
        Params
        ======
            state_size (int): dimension of each state
            action_size (int): dimension of each action
            seed (int): random seed
        """
        self.state_size = state_size
        self.action_size = action_size
        self.seed = random.seed(seed)

        # Q-Network
        self.qnetwork_local = ANN(state_size, action_size, seed).to(device)
        self.qnetwork_target = ANN(state_size, action_size, seed).to(device)
        self.optimizer = optim.Adam(self.qnetwork_local.parameters(), lr=LR)

        # Replay memory
        self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, seed)
        # Initialize time step (for updating every UPDATE_EVERY steps)
        self.t_step = 0

    def step(self, state, action, reward, next_state, done):
        # Save experience in replay memory
        self.memory.add(state, action, reward, next_state, done)

        # Learn every UPDATE_EVERY time steps.
        self.t_step = (self.t_step + 1) % UPDATE_EVERY
        if self.t_step == 0:
            # If enough samples are available in memory, get random subset and learn
            if len(self.memory) > BATCH_SIZE:
                experiences = self.memory.sample()
                self.learn(experiences, GAMMA)

    def act(self, state, eps=0.):
        """Returns actions for given state as per current policy.
        
        Params
        ======
            state (array_like): current state
            eps (float): epsilon, for epsilon-greedy action selection
        """
        state = torch.from_numpy(state).float().unsqueeze(0).to(device)
        self.qnetwork_local.eval()
        with torch.no_grad():
            action_values = self.qnetwork_local(state)
        self.qnetwork_local.train()

        # Epsilon-greedy action selection
        if random.random() > eps:
            return np.argmax(action_values.cpu().data.numpy())
        else:
            return random.choice(np.arange(self.action_size))

    def learn(self, experiences, gamma):
        """Update value parameters using given batch of experience tuples.

        Params
        ======
            experiences (Tuple[torch.Tensor]): tuple of (s, a, r, s', done) tuples 
            gamma (float): discount factor
        """
        states, actions, rewards, next_states, dones = experiences

        ## TODO: compute and minimize the loss
        "*** YOUR CODE HERE ***"
        # states, actions, rewards, next_states, dones = experiences

        # Double Q learning
        best_next_action = self.qnetwork_local(next_states).detach().max(
            1)[1].unsqueeze(1)
        Q_targets_next = self.qnetwork_target(next_states).detach().gather(
            1, best_next_action)

        # Q Learning
        # Get max predicted Q values (for next states) from target model
        # Q_targets_next = self.qnetwork_target(next_states).detach().max(1)[0].unsqueeze(1)

        # Compute Q targets for current states
        Q_targets = rewards + (gamma * Q_targets_next * (1 - dones))

        # Get expected Q values from local model
        Q_expected = self.qnetwork_local(states).gather(1, actions)

        # Compute loss
        loss = F.mse_loss(Q_expected, Q_targets)
        # Minimize the loss
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        # ------------------- update target network ------------------- #
        self.soft_update(self.qnetwork_local, self.qnetwork_target, TAU)

    def soft_update(self, local_model, target_model, tau):
        """Soft update model parameters.
        θ_target = τ*θ_local + (1 - τ)*θ_target

        Params
        ======
            local_model (PyTorch model): weights will be copied from
            target_model (PyTorch model): weights will be copied to
            tau (float): interpolation parameter 
        """
        for target_param, local_param in zip(target_model.parameters(),
                                             local_model.parameters()):
            target_param.data.copy_(tau * local_param.data +
                                    (1.0 - tau) * target_param.data)
Example #7
0
import argparse
import numpy as np
import tensorflow as tf

from model import ANN
import data

parser = argparse.ArgumentParser(description='Visualize ANN')
parser.add_argument('-d',
                    '--dataset',
                    type=str,
                    default='mnist',
                    choices=data.get_names())
parser.add_argument('--num_iter', type=int, default=5000)
args = parser.parse_args()

dataset = data.init_dataset(name=args.dataset)

model = ANN(dataset.shape)

model.train(dataset.tr_data, dataset.tr_labels, num_iter=args.num_iter)
Example #8
0
                          torch.from_numpy(test_subject_labels))

# tensor to DataLoader
train_dataloader = DataLoader(train_data,
                              batch_size=1024,
                              shuffle=True,
                              drop_last=True)
test_dataloader = DataLoader(test_data,
                             batch_size=1024,
                             shuffle=True,
                             drop_last=True)

# ----------------------------------------- Training & Validation ----------------------------------------- #

precision = 1e-8
ann = ANN(input_channel=448, number_of_classes=7).cuda()
class_criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(ann.parameters(), lr=1e-3)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer,
                                                 mode='min',
                                                 factor=0.1,
                                                 patience=6,
                                                 verbose=True,
                                                 eps=precision)

epoch_num = 120
patience = 12
patience_increase = 12
best_loss = float('inf')

for epoch in range(epoch_num):
# print(test_label.shape)
# time.sleep(100)

# numpy to tensor
test_data = TensorDataset(torch.from_numpy(test_data),
                          torch.from_numpy(test_label))

# tensor to DataLoader
test_dataloader = DataLoader(test_data,
                             batch_size=4096,
                             shuffle=True,
                             drop_last=True)
print('Test segments loaded. Batch_size = 4096.\n')

print('Loading trained ANN network parameters...')
ann_test = ANN(input_channel=448, number_of_classes=7).cuda()
ann_test.load_state_dict(torch.load(r'saved_model\ANN_8_subjects.pkl'))
ann_test.eval()

total_batch_num = len(test_dataloader)
running_loss = 0.
correct_gesture_label, total_num = 0.0, 0.0
print('Start testing...\n')

for i, (data, label) in enumerate(test_dataloader):

    data = data.cuda()
    data = data.view(4096, -1)
    gesture_label = label.cuda()

    pred_label = ann_test(data)
Example #10
0
        '--num_workers',
        type=int,
        default=8,
        help='The numbers of processors used for loading the data')

    args = parser.parse_args()

    #device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    os.makedirs('saved_models', exist_ok=True)

    data = DataLoader(Data("./data/training"),
                      batch_size=args.batch_size,
                      shuffle=True,
                      num_workers=args.num_workers)

    model = ANN()
    criterion = torch.nn.L1Loss()
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=0.0002,
                                 betas=(0.9, 0.999))

    if args.epoch > 0:
        model.load_state_dict(
            torch.load('saved_models/model_%d.pth' % args.epoch))

    for epoch in range(args.epoch, args.num_epochs):
        for i, items in enumerate(data):
            init = items['init']
            init_pos, init_vel, init_t = init['pos'], init['vel'], init['t']
            init_t = init_t.view(-1, 1)