def set_seed(args): random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) if args.n_gpu > 0: #torch.cuda.manual_seed_all(args.seed) torch.manual_seed_all(args.seed)
def init_seeds(seed=0): random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) else: torch.manual_seed(seed) torch.manual_seed_all(seed)
def main(): global args, best_prec1 args = parser.parse_args() model=sent_model(embedding_dim=1024,input_dim=1000,num_hidden=1024, num_classes=2) loaded_model=torch.load(args.eval) model.load_state_dict(loaded_model.state_dict()) if args.cuda and torch.cuda.is_available(): torch.cuda.manual_seed_all(args.seed) torch.cuda.set_device(int(args.device_ids[0])) cudnn.benchmark = True model.to('cuda',dtype=torch.float) else: torch.manual_seed_all(args.seed) args.device_ids = None logging.basicConfig(filename='example_sentiment.log',level=logging.DEBUG) #logging.info("created model with configuration: %s", model_config) num_parameters = sum([l.nelement() for l in model.parameters()]) logging.info("number of parameters: %d", num_parameters) criterion = nn.CrossEntropyLoss() criterion = criterion.cuda() val_data = IMDBDataset(data_path='./Datasets',training=False) val_loader = torch.utils.data.DataLoader( val_data, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) # evaluate on validation set val_loss,val_acc = validate(val_loader, model, criterion) print(val_loss,val_acc) logging.info('\n Validation Loss {val_loss:.4f}\t' 'Validation Acc {val_acc:.4f}\n' .format(val_loss=val_loss,val_acc=val_acc))
data_list = args.dataset batch_size = args.batch_size learning_rate = args.lr use_cuda = torch.cuda.is_available() if use_cuda: torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) torch.cuda.manual_seed(args.seed) np.random.seed(args.seed) random.seed(args.seed) torch.manual_seed(args.seed) torch.backends.cudnn.deterministic = True else: torch.manual_seed(args.seed) torch.manual_seed_all(args.seed) torch.manual_seed(args.seed) np.random.seed(args.seed) random.seed(args.seed) torch.manual_seed(args.seed) print(f'lr:{args.lr},step:{args.step_size},gamma:{args.gamma}') for data_name in data_list: print(data_name) vocab_dict_path = args.word_embed_file file_path = args.data_path + data_name + '.json' glove_data = 'data/' + data_name + '_.glove_data.pkl' glove_matrix = 'data/' + data_name + '_glove_matrix.pkl'
parser = argparse.ArgumentParser(description='MNIST with CNN') parser.add_argument('--epochs', '-e', type=int, default=15) parser.add_argument('--batch', '-b', type=int, default=100) parser.add_argument('--lr', '-l', type=float, help='learning rate', default=1e-5) args = parser.parse_args() # for reprodecibility random.seed(777) torch.manual_seed(777) if device == 'cuda': torch.manual_seed_all(777) # hyperparameters training_epochs = 15 batch_size = 100 # MNIST dataset mnist_train = dsets.MNIST(root='MNIST_data/', train=True, transform=transforms.ToTensor(), download=False) mnist_test = dsets.MNIST(root='MNIST_data/', train=False, transform=transforms.ToTensor(), download=False)
# CUDA for PyTorch use_cuda = torch.cuda.is_available() device = torch.device("cuda:0" if use_cuda else "cpu") print(torch.cuda.get_device_name(device)) torch.backends.cudnn.benchmark = True # Setting the seeds for reproducibility np.random.seed(42) if use_cuda: torch.cuda.manual_seed_all(42) else: torch.manual_seed_all(42) # ### Data preprocessing # # As the training data and the test data are formatted differently, we must either preprocess the data such that the formats of both sets are the same, or ensure that our model is capable of predicting on the two different formats. We went with the first option because it is less time consuming to implement. # # We did this by splitting the training data into segments the same size as the test data segments, i.e. 150000 data points each. Each segment is labeled with a single `time_to_failure` corresponding to the time between the last row of the segment and the next laboratory earthquake. We then put each of these segments into a single dataframe, and saved this as a pickle file to be used as our training data. # # Following this, we merged the separate test segments into another single dataframe, and saved this as a pickle file to be used as our test data. # # As the dataset is massive, we used `Joblib` to help run the functions as a pipeline jobs with parallel computing. The feature extraction code is in `./utils/lanl_data.py`. # In[3]: