test_loader = data.create_dataset('test', seed, realizations, root_bn, root_Pk, batch_size) # get the number of elements in the test set size = 0 for x, y in test_loader: size += x.shape[0] # define the array with the results pred = np.zeros((size, 5), dtype=np.float32) true = np.zeros((size, 5), dtype=np.float32) # get the parameters of the trained model #model = architecture.model_1hl(bins_SFRH, h1, 6, dropout_rate) #model = architecture.model_2hl(bins_SFRH, h1, h2, 6, dropout_rate) model = architecture.model_3hl(512 + 158, h1, h2, h3, 5, dropout_rate) model.load_state_dict(torch.load(fmodel)) model.to(device=device) # loop over the different batches and get the prediction offset = 0 model.eval() for x, y in test_loader: with torch.no_grad(): x = x.to(device) y = y.to(device) y_NN = model(x) length = x.shape[0] pred[offset:offset + length] = y_NN.cpu().numpy() true[offset:offset + length] = y.cpu().numpy() offset += length
test_loader = data.create_dataset('test', seed, realizations, root_in, bins_SFRH, sim, batch_size, root_out) # get the number of elements in the test set size = 0 for params_test, SFRH_test in test_loader: size += params_test.shape[0] # define the array with the results pred = np.zeros((size, 6), dtype=np.float32) true = np.zeros((size, 6), dtype=np.float32) # get the parameters of the trained model #model = architecture.model_1hl(bins_SFRH, h1, 6, dropout_rate) #model = architecture.model_2hl(bins_SFRH, h1, h2, 6, dropout_rate) model = architecture.model_3hl(bins_SFRH, h1, h2, h3, 6, dropout_rate) model.load_state_dict(torch.load(fmodel)) model.to(device=device) # loop over the different batches and get the prediction offset = 0 model.eval() for params_test, SFRH_test in test_loader: with torch.no_grad(): #params_test = params_test.to(device) SFRH_test = SFRH_test.to(device) params_pred = model(SFRH_test) length = params_test.shape[0] pred[offset:offset + length] = params_pred.cpu().numpy() true[offset:offset + length] = params_test.numpy() offset += length
# define loss function criterion = nn.MSELoss() # get train, validation, and test sets print('preparing dataset loaders') train_loader = data.create_dataset('train', seed, realizations, root_in, bins_SFRH, sim, batch_size, root_out) valid_loader = data.create_dataset('valid', seed, realizations, root_in, bins_SFRH, sim, batch_size, root_out) test_loader = data.create_dataset('test', seed, realizations, root_in, bins_SFRH, sim, batch_size, root_out) # define architecture #model = architecture.model_1hl(6, h1, bins_SFRH, dr) model = architecture.model_2hl(6, h1, h2, bins_SFRH, dr) model = architecture.model_3hl(6, h1, h2, h3, bins_SFRH, dr) model.to(device=device) network_total_params = sum(p.numel() for p in model.parameters()) print('total number of parameters in the model = %d' % network_total_params) # define optimizer and scheduler optimizer = torch.optim.Adam(model.parameters(), lr=lr, betas=(0.5, 0.999), weight_decay=wd) # load best-model, if it exists if os.path.exists(fmodel): print('Loading model...') model.load_state_dict(torch.load(fmodel)) model.to(device=device)