MCPcopy Create free account
hub / github.com/thuml/Time-Series-Library / train

Method train

exp/exp_classification.py:79–143  ·  view source on GitHub ↗
(self, setting)

Source from the content-addressed store, hash-verified

77 return total_loss, accuracy
78
79 def train(self, setting):
80 train_data, train_loader = self._get_data(flag='TRAIN')
81 vali_data, vali_loader = self._get_data(flag='TEST')
82 test_data, test_loader = self._get_data(flag='TEST')
83
84 path = os.path.join(self.args.checkpoints, setting)
85 if not os.path.exists(path):
86 os.makedirs(path)
87
88 time_now = time.time()
89
90 train_steps = len(train_loader)
91 early_stopping = EarlyStopping(patience=self.args.patience, verbose=True)
92
93 model_optim = self._select_optimizer()
94 criterion = self._select_criterion()
95
96 for epoch in range(self.args.train_epochs):
97 iter_count = 0
98 train_loss = []
99
100 self.model.train()
101 epoch_time = time.time()
102
103 for i, (batch_x, label, padding_mask) in enumerate(train_loader):
104 iter_count += 1
105 model_optim.zero_grad()
106
107 batch_x = batch_x.float().to(self.device)
108 padding_mask = padding_mask.float().to(self.device)
109 label = label.to(self.device)
110
111 outputs = self.model(batch_x, padding_mask, None, None)
112 loss = criterion(outputs, label.long().squeeze(-1))
113 train_loss.append(loss.item())
114
115 if (i + 1) % 100 == 0:
116 print("\titers: {0}, epoch: {1} | loss: {2:.7f}".format(i + 1, epoch + 1, loss.item()))
117 speed = (time.time() - time_now) / iter_count
118 left_time = speed * ((self.args.train_epochs - epoch) * train_steps - i)
119 print('\tspeed: {:.4f}s/iter; left time: {:.4f}s'.format(speed, left_time))
120 iter_count = 0
121 time_now = time.time()
122
123 loss.backward()
124 nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=4.0)
125 model_optim.step()
126
127 print("Epoch: {} cost time: {}".format(epoch + 1, time.time() - epoch_time))
128 train_loss = np.average(train_loss)
129 vali_loss, val_accuracy = self.vali(vali_data, vali_loader, criterion)
130 test_loss, test_accuracy = self.vali(test_data, test_loader, criterion)
131
132 print(
133 "Epoch: {0}, Steps: {1} | Train Loss: {2:.3f} Vali Loss: {3:.3f} Vali Acc: {4:.3f} Test Loss: {5:.3f} Test Acc: {6:.3f}"
134 .format(epoch + 1, train_steps, train_loss, vali_loss, val_accuracy, test_loss, test_accuracy))
135 early_stopping(-val_accuracy, self.model, path)
136 if early_stopping.early_stop:

Callers 1

valiMethod · 0.45

Calls 8

_get_dataMethod · 0.95
_select_optimizerMethod · 0.95
_select_criterionMethod · 0.95
valiMethod · 0.95
EarlyStoppingClass · 0.90
stepMethod · 0.80
loadMethod · 0.80
backwardMethod · 0.45

Tested by

no test coverage detected