def main(config): device = torch.device('cpu') if config.gpu_id < 0 else torch.device( 'cuda:%d' % config.gpu_id) x, y = load_mnist(is_train=True) x = x.view(x.size(0), -1) train_cnt = int(x.size(0) * config.train_rate) valid_cnt = x.size(0) - train_cnt indices = torch.randperm(x.size(0)) x = torch.index_select(x, dim=0, index=indices).to(device).split( [train_cnt, valid_cnt], dim=0) print("Train:", x[0].shape, y[0].shape) print("Valid:", x[1].shape, y[1].shape) model = ImageClassifier(28**2, 10).to(device) optimizer = optim.Adam(model.parameters()) crit = nn.CrossEntropyLoss() trainer = Trainer(model, optimizer, crit) trainer.train((x[0], y[0]), (x[1], y[1]), config) torch.save({ 'model': trainer.model.state_dict(), 'config': config, }, config.model_fn)
def main(config): # Set device based on user defined configuration. device = torch.device('cpu') if config.gpu_id < 0 else torch.device( 'cuda:%d' % config.gpu_id) x, y = load_mnist(is_train=True) # Reshape tensor to chunk of 1-d vectors. x = x.view(x.size(0), -1) train_cnt = int(x.size(0) * config.train_ratio) valid_cnt = x.size(0) - train_cnt # Shuffle dataset to split into train/valid set. indices = torch.randperm(x.size(0)) x = torch.index_select(x, dim=0, index=indices).to(device).split( [train_cnt, valid_cnt], dim=0) y = torch.index_select(y, dim=0, index=indices).to(device).split( [train_cnt, valid_cnt], dim=0) print("Train:", x[0].shape, y[0].shape) print("Valid:", x[1].shape, y[1].shape) model = ImageClassifier(28**2, 10).to(device) optimizer = optim.Adam(model.parameters()) crit = nn.NLLLoss() trainer = Trainer(model, optimizer, crit) trainer.train((x[0], y[0]), (x[1], y[1]), config) # Save best model weights. torch.save({ 'model': trainer.model.state_dict(), 'config': config }, config.model_fn)
def main(config): device = torch.device('cpu') if config.gpu_id < 0 else torch.device('cuda:%d' % config.gpu_id) train_loader, valid_loader, test_loader = get_loaders(config) print("Train:", len(train_loader.dataset)) print("Valid:", len(valid_loader.dataset)) print("Test:", len(test_loader.dataset)) model = ImageClassifier(28**2, 10).to(device) optimizer = optim.Adam(model.parameters()) crit = nn.CrossEntropyLoss() trainer = Trainer(config) trainer.train(model, crit, optimizer, train_loader, valid_loader)
def main(config): device = torch.device('cpu') if config.gpu_id < 0 else torch.device( 'cuda : %d' % config.gpu_id) train_loader, valid_loader, test_loader = get_loaders(config) # get_loader를 통해 데이터를 받아옴 print('Train', len(train_loader.dataset)) print('Valid', len(valid_loader.dataset)) print('Test', len(test_loader.dataset)) model = ImageClassifier(28**2, 10).to(device) # input = (784)이고 10개의 클래스로 분류할 것 optimizer = optim.Adam(model.parameters()) crit = nn.CrossEntropyLoss() # Loss Function trainer = Trainer(config) trainer.train(model, crit, optimizer, train_loader, valid_loader)
def main(config): # Set device based on user defined configuration. device = torch.device('cpu') if config.gpu_id < 0 else torch.device('cuda:%d' % config.gpu_id) x, y = load_mnist(is_train=True, flatten=True) x, y = split_data(x.to(device), y.to(device), train_ratio=config.train_ratio) print("Train:", x[0].shape, y[0].shape) print("Valid:", x[1].shape, y[1].shape) input_size = int(x[0].shape[-1]) output_size = int(max(y[0])) + 1 model = ImageClassifier( input_size=input_size, output_size=output_size, hidden_sizes=get_hidden_sizes(input_size, output_size, config.n_layers), use_batch_norm=not config.use_dropout, dropout_p=config.dropout_p, ).to(device) optimizer = optim.Adam(model.parameters()) crit = nn.NLLLoss() if config.verbose >= 1: print(model) print(optimizer) print(crit) trainer = Trainer(model, optimizer, crit) trainer.train( train_data=(x[0], y[0]), valid_data=(x[1], y[1]), config=config ) # Save best model weights. torch.save({ 'model': trainer.model.state_dict(), 'opt': optimizer.state_dict(), 'config': config, }, config.model_fn)
def main(config): # Set device based on user defined configuration. device = torch.device('cpu') if config.gpu_id < 0 else torch.device( 'cuda:%d' % config.gpu_id) train_loader, valid_loader, test_loader = get_loaders(config) print("Train:", len(train_loader.dataset)) print("Valid:", len(valid_loader.dataset)) print("Test:", len(test_loader.dataset)) model = ImageClassifier(28**2, 10).to( device) # 입력은 784, 10개 클래스로 분류, GPU면 여기로 옮겨줘? optimizer = optim.Adam(model.parameters( )) # model.parameters() 하면 모델 내 웨이트 파라미터들이 이터레이티브하게 나온다 crit = nn.CrossEntropyLoss() # 분류를 해야하기 때문에 교차엔트로피 쓴다, 모델에서 소프트맥스로 끝나는 이유 trainer = Trainer(config) # 이그나이트를 써서 짜놓은 메서드 trainer.train(model, crit, optimizer, train_loader, valid_loader)
def main(config): # 연산 디바이스 설정 if config.gpu_id < 0: print("Device: CPU") device = torch.device('cpu') else: print("Device:", torch.cuda.get_device_name(0)) device = torch.device('cuda:%d' % config.gpu_id) # 입력값에 따라 학습, 검증 데이터 분할 x, y = load_mnist(is_train=True, flatten=True) train_cnt = int(x.size(0) * config.train_ratio) valid_cnt = x.size(0) - train_cnt # 각 데이터셋 셔플링 indices = torch.randperm(x.size(0)) x = torch.index_select(x, dim=0, index=indices).to(device).split( [train_cnt, valid_cnt], dim=0) y = torch.index_select(y, dim=0, index=indices).to(device).split( [train_cnt, valid_cnt], dim=0) print("Train:", x[0].shape, y[0].shape) print("Valid:", x[1].shape, y[1].shape) # 모델 선언 및 구조 결정 model = ImageClassifier(28 * 28, 10).to(device) optimizer = optim.Adam(model.parameters()) crit = nn.CrossEntropyLoss() # 학습 수행 trainer = Trainer(model, optimizer, crit) trainer.train(train_data=(x[0], y[0]), valid_data=(x[1], y[1]), config=config) #모델 저장 torch.save({ 'model': trainer.model.state_dict(), 'config': config }, config.model_fn)
def main(config): # 실행 장치 설정 device = torch.device('cpu') if config.gpu_id < 0 else torch.device( 'cuda:%d' % config.gpu_id) # 데이터 로드 x, y = load_mnist(is_train=True) x = x.view(x.size(0), -1) # flatten 된 형태 train_cnt = int(x.size(0) * config.train_ratio) valid_cnt = x.size(0) - train_cnt # Shuffle & Random Split indices = torch.randperm(x.size(0)) x = torch.index_select(x, dim=0, index=indices).to(device).split( [train_cnt, valid_cnt], dim=0) y = torch.index_select(y, dim=0, index=indices).to(device).split( [train_cnt, valid_cnt], dim=0) # split 되는 형태: [(train_cnt, 784) , (valid_cnt, 784)] print("Train : ", x[0].shape, y[0].shape) print("Valid : ", x[1].shape, y[1].shape) # load model, optimizer, criterion model = ImageClassifier(28**2, 10).to(device) optimizer = optim.Adam(model.parameters()) criterion = nn.CrossEntropyLoss() trainer = Trainer(model, optimizer, criterion) trainer.train((x[0], y[0]), (x[1], y[1]), config) # 베스트 모델 및 가중치 저장 torch.save({ 'model': trainer.model.state_dict(), 'config': config, }, config.model_fn)