import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from Model import Net model = Net(10) #10 classes criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) from dataset import MeshData DataObject = MeshData( '/home/prathmesh/Desktop/SoC-2020/ModelNet10_stl/ModelNet10') dataLoad = torch.utils.data.DataLoader(DataObject, batch_size=1, shuffle=True) batch = next(iter(dataLoad)) print(len(batch)) max_epochs = 30 loss_list = [] for epochs in range(max_epochs): #print('e =',epochs) running_loss = 0.0 for i, data in enumerate(dataLoad, 0): x, y = data x = x[0].float().to(device) #y = y.float() y = y[0].to(device) optimizer.zero_grad()
np.random.shuffle(indices) train_indices, val_indices = indices[split:], indices[:split] train_sampler = SubsetRandomSampler(train_indices) valid_sampler = SubsetRandomSampler(val_indices) train_load = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=train_sampler) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device) net = Net() net.to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.7) # Training for epoch in range(5): for i, data in enumerate(train_load, 0): inputs, labels, name = data inputs = inputs.float() inputs, labels = inputs.to(device), labels.to(device) # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = net(inputs) loss = criterion(outputs, labels) loss.backward()
class Trainer: def __init__(self, hidden_dim, buffer_size, gamma, batch_size, device, writer): self.env = make("connectx", debug=True) self.device = device self.policy = Net(self.env.configuration.columns * self.env.configuration.rows, hidden_dim, self.env.configuration.columns).to( device) self.target = Net(self.env.configuration.columns * self.env.configuration.rows, hidden_dim, self.env.configuration.columns).to( device) self.enemyNet = Net(self.env.configuration.columns * self.env.configuration.rows, hidden_dim, self.env.configuration.columns).to( device) self.target.load_state_dict(self.policy.state_dict()) self.target.eval() self.buffer = ExperienceReplay(buffer_size) self.enemy = "random" self.trainingPair = self.env.train([None, self.enemy]) self.loss_function = nn.MSELoss() self.optimizer = optim.Adam(params=self.policy.parameters(), lr=0.001) self.gamma = gamma self.batch_size = batch_size self.first = True self.player = 1 self.writer = writer def agent(self, observation, configuration): with torch.no_grad(): state = torch.tensor(observation['board'], dtype=torch.float) reshaped = self.reshape(state) action = self.takeAction(self.enemyNet(reshaped).view(-1), reshaped, 0, False) return action def switch(self): self.trainingPair = self.env.train([None, "negamax"]) self.enemy = "negamax" def switchPosition(self): self.env.reset() if self.first: self.trainingPair = self.env.train([self.enemy, None]) self.player = 2 else: self.trainingPair = self.env.train([None, self.enemy]) self.player = 1 self.first = not self.first def load(self, path): self.policy.load_state_dict(torch.load(path)) def synchronize(self): self.target.load_state_dict(self.policy.state_dict()) def save(self, name): torch.save(self.policy.state_dict(), name) def reset(self): self.env.reset() return self.trainingPair.reset() def step(self, action): return self.trainingPair.step(action) def addExperience(self, experience): self.buffer.append(experience) def epsilon(self, maxE, minE, episode, lastEpisode): return (maxE - minE) * max((lastEpisode - episode) / lastEpisode, 0) + minE def change_reward(self, reward, done): if done and reward == 1: return 10 if done and reward == -1: return -10 if reward is None and done: return -20 if done: return 1 if reward == 0: return 1 / 42 else: return reward def change_reward_streak(self, reward, done, reshapedBoard, action, useStreak): if done and reward == 1: return 20 if done and reward == -1: return -20 if reward is None and done: return -40 if done: return 1 if reward == 0 & useStreak: return 1 / 42 + self.streakReward(self.player, reshapedBoard, action) if reward == 0: return 1 / 42 else: return reward def streakReward(self, player, reshapedBoard, action): verticalReward = 0 horizontalReward = 0 if self.longestVerticalStreak(player, reshapedBoard, action) == 3: verticalReward = 3 if self.longestHorizontalStreak(player, reshapedBoard, action) == 3: horizontalReward = 3 return verticalReward + horizontalReward + self.longestDiagonalStreak(player, reshapedBoard, action) def longestVerticalStreak(self, player, reshapedBoard, action): count = 0 wasZero = False for i in range(5, 0, -1): if reshapedBoard[0][player][i][action] == 0: wasZero = True if reshapedBoard[0][player][i][action] == 1 & wasZero: count = 0 wasZero = False count += reshapedBoard[0][player][i][action] if reshapedBoard[0][0][0][action] == 0: return 0 return count def longestHorizontalStreak(self, player, reshapedBoard, action): count = 0 rowOfAction = self.rowOfAction(player, reshapedBoard, action) wasZero = False for i in range(7): if reshapedBoard[0][player][rowOfAction][i] == 0: wasZero = True if reshapedBoard[0][player][rowOfAction][i] == 1 & wasZero: count = 0 wasZero = False count += reshapedBoard[0][player][rowOfAction][i] return count def longestDiagonalStreak(self, player, reshapedBoard, action): rowOfAction = self.rowOfAction(player, reshapedBoard, action) for row in range(4): for col in range(5): if reshapedBoard[0][player][row][col] == reshapedBoard[0][player][row + 1][col + 1] == \ reshapedBoard[0][player][row + 2][col + 2] == 1 and self.actionInDiagonal1(action, row, col, rowOfAction): return 3 for row in range(5, 1, -1): for col in range(4): if reshapedBoard[0][player][row][col] == reshapedBoard[0][player][row - 1][col + 1] == \ reshapedBoard[0][player][row - 2][col + 2] == 1 and self.actionInDiagonal2(action, row, col, rowOfAction): return 3 return 0 def actionInDiagonal1(self, action, row, col, rowOfAction): return (rowOfAction == row and action == col or rowOfAction == row + 1 and action == col + 1 or rowOfAction == row + 2 and action == col + 2) def actionInDiagonal2(self, action, row, col, rowOfAction): return (rowOfAction == row and action == col or rowOfAction == row - 1 and action == col + 1 or rowOfAction == row - 2 and action == col + 2) def rowOfAction(self, player, reshapedBoard, action): rowOfAction = 10 for i in range(6): if reshapedBoard[0][player][i][action] == 1: rowOfAction = min(i, rowOfAction) return rowOfAction def policyAction(self, board, episode, lastEpisode, minEp=0.1, maxEp=0.9): reshaped = self.reshape(torch.tensor(board)) output = self.policy(reshaped).view(-1) return self.takeAction(output, reshaped, self.epsilon(maxEp, minEp, episode, lastEpisode)) def takeAction(self, actionList: torch.tensor, board, epsilon, train=True): if (np.random.random() < epsilon) & train: # invalide actions rein=geht nicht #return torch.tensor(np.random.choice(len(actionList))).item() return np.random.choice([i for i in range(len(actionList)) if board[0][0][0][i] == 1]) else: for i in range(7): if board[0][0][0][i] == 0: actionList[i] = float('-inf') return torch.argmax(actionList).item() def reshape(self, board: torch.tensor, unsqz=True): tensor = board.view(-1, 7).long() # [0] = wo kann er reinwerfen(da wo es geht, steht eine 1), [1] = player1 (da wo es geht steht eine 0), [2] = player2 (da wo es geht steht eine 0) a = F.one_hot(tensor, 3).permute([2, 0, 1]) b = a[:, :, :] if unsqz: return torch.unsqueeze(b, 0).float().to(self.device) return b.float().to(self.device) def preprocessState(self, state): state = self.reshape(torch.tensor(state), True) return state def trainActionFromPolicy(self, state, action): state = self.preprocessState(state) value = self.policy(state).view(-1).to(self.device) return value[action].to(self.device) def trainActionFromTarget(self, next_state, reward, done): next_state = self.preprocessState(next_state) target = self.target(next_state) target = torch.max(target, 1)[0].item() target = reward + ((self.gamma * target) * (1 - done)) return torch.tensor(target).to(self.device) def train(self): if len(self.buffer) > self.batch_size: self.optimizer.zero_grad() states, actions, rewards, next_states, dones = self.buffer.sample(self.batch_size, self.device) meanLoss = 0 for i in range(self.batch_size): value = self.trainActionFromPolicy(states[i], actions[i]) target = self.trainActionFromTarget(next_states[i], rewards[i], dones[i]) loss = self.loss_function(value, target) loss.backward() meanLoss += loss self.optimizer.step() return meanLoss / self.batch_size
preprcess.Save() print('wordbag: ') print(preprcess.wordbag) print('label_list: ') print(preprcess.label_list) x = np.array(preprcess.wordbag) y = np.array(preprcess.label_list) x = torch.from_numpy(x).type(torch.FloatTensor) y = torch.from_numpy(y) x, y = Variable(x), Variable(y) net = Net() optimizer = torch.optim.ASGD(net.parameters(), lr=0.002) criterion = torch.nn.CrossEntropyLoss() for t in range(10000): out = net(x) loss = criterion(out, y) optimizer.zero_grad() loss.backward() optimizer.step() if t % 100 == 0: prediction = torch.max(F.softmax(out), 1)[1] pred_y = prediction.data.numpy().squeeze() target_y = y.data.numpy() accuracy = sum(pred_y == target_y) / 652 print('------------------')
acc_total = [] pred_total = [] true_total = [] for i in tqdm(range(TOTAL)): image_shape = full_dataset.x_data.shape[1:] device = torch.device(CUDA_N if torch.cuda.is_available() else 'cpu') torch.manual_seed(SEED[i]) net = Net(image_shape, NUM_CLASS) net.to(device) print(net) softmax = nn.Softmax() criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.1) loss_list = [] train_acc_list = [] test_acc_list = [] pred_temp = [] true_temp = [] for epoch in range(EPOCH): net.train() running_loss = 0 total = train_size correct = 0 for step, images_labels in enumerate(train_loader):
model = nn.DataParallel(model) if args.reloadModel: model_fp = "model4/resnet18_model2_17.pth" model.load_state_dict(torch.load(model_fp)['modelRoadMap_state_dict']) print("model_loaded") model.to(device) # In[ ]: # Optimizer optimizer = optim.Adam(model.parameters(), lr=args.lr) # In[ ]: # loss func def modify_output_for_loss_fn(loss_fn, output, dim): if loss_fn == "ce": return output if loss_fn == "mse": return F.softmax(output, dim=dim) if loss_fn == "nll": return F.log_softmax(output, dim=dim) if loss_fn in ["bce", "wbce", "wbce1"]:
from torch.utils.data import DataLoader from torchvision import transforms composed = transforms.Compose([Normalize(), ToTensor()]) trainset = ImagesDataset(csv_file=root_dir + 'train.csv', root_dir=root_dir, transform=composed) trainloader = DataLoader(trainset, batch_size=4, shuffle=True, num_workers=4) """ Train data """ import torch.optim as optim criterion = nn.MSELoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9, weight_decay=1e-6) for epoch in range(num_epoch): running_loss = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data['image'], data['label'] inputs, labels = inputs.to(device), labels.to(device) optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, labels) loss.backward()
def main(): global opt, model opt = parser.parse_args() print(opt) cuda = opt.cuda if cuda and not torch.cuda.is_available(): raise Exception("No GPU found, please run without --cuda") opt.seed = random.randint(1, 10000) print("Random Seed: ", opt.seed) torch.manual_seed(opt.seed) if cuda: torch.cuda.manual_seed(opt.seed) cudnn.benchmark = True print("===> Loading datasets") train_set = DatasetFromHdf5("data/train.h5") training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True) print("===> Building model") model = Net() criterion = nn.MSELoss(size_average=False) print("===> Setting GPU") if cuda: model = torch.nn.DataParallel(model).cuda() criterion = criterion.cuda() # optionally resume from a checkpoint if opt.resume: if os.path.isfile(opt.resume): print("=> loading checkpoint '{}'".format(opt.resume)) checkpoint = torch.load(opt.resume) opt.start_epoch = checkpoint["epoch"] + 1 model.load_state_dict(checkpoint["model"].state_dict()) else: print("=> no checkpoint found at '{}'".format(opt.resume)) # optionally copy weights from a checkpoint if opt.pretrained: if os.path.isfile(opt.pretrained): print("=> loading model '{}'".format(opt.pretrained)) weights = torch.load(opt.pretrained) model.load_state_dict(weights['model'].state_dict()) else: print("=> no model found at '{}'".format(opt.pretrained)) print("===> Setting Optimizer") optimizer = optim.SGD(model.parameters(), lr=opt.lr, momentum=opt.momentum, weight_decay=opt.weight_decay) print("===> Training") for epoch in range(opt.start_epoch, opt.nEpochs + 1): train(training_data_loader, optimizer, model, criterion, epoch) save_checkpoint(model, epoch)