def model_fn(model_dir): """Load the PyTorch model from the `model_dir` directory.""" print("Loading model.") # First, load the parameters used to create the model. model_info = {} model_info_path = os.path.join(model_dir, 'model_info.pth') with open(model_info_path, 'rb') as f: model_info = torch.load(f) print("model_info: {}".format(model_info)) # Determine the device and construct the model. device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = BinaryClassifier(model_info['input_features'], model_info['hidden_dim'], model_info['output_dim']) # Load the stored model parameters. model_path = os.path.join(model_dir, 'model.pth') with open(model_path, 'rb') as f: model.load_state_dict(torch.load(f)) # set to eval mode, could use no_grad model.to(device).eval() print("Done loading model.") return model
def model_fn(model_dir): """Load the PyTorch model from the `model_dir` directory.""" print("Loading model.") model_info = {} model_info_path = os.path.join(model_dir, 'model_info.pth') with open(model_info_path, 'rb') as f: model_info = torch.load(f) print("model_info: {}".format(model_info)) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = BinaryClassifier(model_info['input_features'], model_info['hidden_dim'], model_info['output_dim']) model_path = os.path.join(model_dir, 'model.pth') with open(model_path, 'rb') as f: model.load_state_dict(torch.load(f)) model.to(device).eval() print("Done loading model.") return model
def model_fn(model_dir): """Load the PyTorch model from the `model_dir` directory.""" print("Loading model.") # First, load the parameters used to create the model. model_info = {} model_info_path = os.path.join(model_dir, "model_info.pth") with open(model_info_path, "rb") as f: model_info = torch.load(f) print("model_info: {}".format(model_info)) # Determine the device and construct the model. device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = BinaryClassifier( model_info["input_features"], model_info["hidden_dim"], model_info["output_dim"], model_info["momentum"], model_info["dropout_rate"], model_info["num_layers"], ) # Load the store model parameters. model_path = os.path.join(model_dir, "model.pth") with open(model_path, "rb") as f: model.load_state_dict(torch.load(f)) # Prep for testing model.to(device).eval() print("Done loading model.") return model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print("Using device {}.".format(device)) torch.manual_seed(args.seed) # Load the training data. train_loader = _get_train_data_loader(args.batch_size, args.data_dir) ## --- Your code here --- ## ## TODO: Build the model by passing in the input params # To get params from the parser, call args.argument_name, ex. args.epochs or ards.hidden_dim # Don't forget to move your model .to(device) to move to GPU , if appropriate model = BinaryClassifier(args.input_features, args.hidden_dim, args.output_dim) model = model.to(device) ## TODO: Define an optimizer and loss function for training optimizer = optim.Adam(model.parameters()) criterion = torch.nn.BCELoss() # Trains the model (given line of code, which calls the above training function) train(model, train_loader, args.epochs, criterion, optimizer, device) ## TODO: complete in the model_info by adding three argument names, the first is given # Keep the keys of this dictionary as they are model_info_path = os.path.join(args.model_dir, 'model_info.pth') with open(model_info_path, 'wb') as f: model_info = { 'input_features': args.input_features, 'hidden_dim': args.hidden_dim, 'output_dim': args.output_dim,
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print("Using device {}.".format(device)) torch.manual_seed(args.seed) # Load the training data. train_loader = _get_train_data_loader(args.batch_size, args.data_dir) ## --- Your code here --- ## ## TODO: Build the model by passing in the input params # To get params from the parser, call args.argument_name, ex. args.epochs or ards.hidden_dim # Don't forget to move your model .to(device) to move to GPU , if appropriate model = BinaryClassifier(input_features = args.input_features, hidden_dim = args.hidden_dim, output_dim = args.output_dim) model.to(device) ## TODO: Define an optimizer and loss function for training optimizer = optim.Adam(model.parameters(), lr= args.lr) criterion = nn.BCELoss() # Trains the model (given line of code, which calls the above training function) train(model, train_loader, args.epochs, criterion, optimizer, device) ## TODO: complete in the model_info by adding three argument names, the first is given # Keep the keys of this dictionary as they are model_info_path = os.path.join(args.model_dir, 'model_info.pth') with open(model_info_path, 'wb') as f: model_info = { 'input_features': args.input_features, 'hidden_dim': args.hidden_dim,