-
Notifications
You must be signed in to change notification settings - Fork 0
/
train (3).py
166 lines (130 loc) · 6.43 KB
/
train (3).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import argparse
import json
import os
import pandas as pd
import torch
import torch.optim as optim
import torch.utils.data
import numpy as np
import matplotlib.pyplot as plt
# imports the model in model.py by name
from model import LSTMClassifier
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 = LSTMClassifier(model_info['input_dim'], model_info['hidden_dim'], model_info['num_layers'], 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
# Gets training data in batches from the train.csv file
def _get_train_data_loader(batch_size, training_dir):
print("Get train data loader.")
train_data = pd.read_csv(os.path.join(training_dir, "train.csv"), header=None, names=None)
train_y = torch.from_numpy(train_data[[0]].values).type(torch.Tensor)
train_x = train_data.drop([0], axis=1).to_numpy()
train_x = train_x.reshape(train_data.shape[0],-1,1)
train_x = torch.from_numpy(train_x).type(torch.Tensor)
train_ds = torch.utils.data.TensorDataset(train_x, train_y)
return torch.utils.data.DataLoader(train_ds, batch_size=batch_size)
return train_data
# Provided training function
def train(model, train_loader, epochs, criterion, optimizer, device):
"""
This is the training method that is called by the PyTorch training script. The parameters
passed are as follows:
model - The PyTorch model that we wish to train.
train_loader - The PyTorch DataLoader that should be used during training.
epochs - The total number of epochs to train for.
criterion - The loss function used for training.
optimizer - The optimizer to use during training.
device - Where the model and data should be loaded (gpu or cpu).
"""
# training loop is provided
for epoch in range(1, epochs + 1):
model.train() # Make sure that the model is in training mode.
total_loss = 0
##loss = []
for batch in train_loader:
# get data
batch_x, batch_y = batch
batch_x = batch_x.to(device)
batch_y = batch_y.to(device)
optimizer.zero_grad()
# get predictions from model
###batch_x = batch_x.reshape(len(batch),-1,1)
y_pred = model(batch_x)
# perform backprop
loss = criterion(y_pred, batch_y)
loss.backward()
optimizer.step()
total_loss += loss.data.item()
loss = loss.np.append(total_loss / len(train_loader))
print("Epoch: {}, MSE Loss: {}".format(epoch, total_loss / len(train_loader)))
print(loss)
if __name__ == '__main__':
# All of the model parameters and training parameters are sent as arguments
# when this script is executed, during a training job
# Here we set up an argument parser to easily access the parameters
parser = argparse.ArgumentParser()
# SageMaker parameters, like the directories for training data and saving models; set automatically
# Do not need to change
parser.add_argument('--output-data-dir', type=str, default=os.environ['SM_OUTPUT_DATA_DIR'])
parser.add_argument('--model-dir', type=str, default=os.environ['SM_MODEL_DIR'])
parser.add_argument('--data-dir', type=str, default=os.environ['SM_CHANNEL_TRAINING'])
# Training Parameters, given
parser.add_argument('--batch-size', type=int, default=10, metavar='N',
help='input batch size for training (default: 10)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
# Model Parameters
parser.add_argument('--input_dim', type=int, default=1, metavar='N',
help='size of the input dimension (default: 1)')
parser.add_argument('--hidden_dim', type=int, default=100, metavar='N',
help='size of the hidden dimension (default: 100)')
parser.add_argument('--output_dim', type=int, default=1, metavar='N',
help='size of the output dimension (default: 1)')
parser.add_argument('--num_layers', type=int, default=2, metavar='N',
help='number of layers (default: 2)')
# args holds all passed-in arguments
args = parser.parse_args()
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)
# 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 = LSTMClassifier(args.input_dim,args.hidden_dim, args.num_layers, args.output_dim).to(device)
## TODO: Define an optimizer and loss function for training
optimizer = optim.Adam(model.parameters())
criterion = torch.nn.MSELoss()
# Trains the model (given line of code, which calls the above training function)
# 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 = {
'num_layers': args.num_layers,
'hidden_dim': args.hidden_dim,
'output_dim': args.output_dim,
'input_dim': args.input_dim,
}
torch.save(model_info, f)
# Save the model parameters
model_path = os.path.join(args.model_dir, 'model.pth')
with open(model_path, 'wb') as f:
torch.save(model.cpu().state_dict(), f)