def fit_eval(self): train, test = self.data.train_test_split() loss = self.model.learn(train[0], train[1]) preds = self.model.predict(test[0]) crit = self.model.criterion() val_loss = crit(to_torch(preds), to_torch(test[1])).item() acc = -1 try: acc = accuracy_score(test[1], preds) except: pass out = {'accuracy': acc, 'training_loss': loss, 'val_loss': val_loss} out['val_vc'] = np.unique(test[1], return_counts=True) out['val_pred_vc'] = np.unique(preds, return_counts=True) return out
def learn(self, x, y): x, y = to_torch(x), to_torch(y) criterion = self.criterion() optimizer = self.optimizer(self.parameters(), lr=self.lr, weight_decay=self.wd) closs = 100000 for i in range(self.tr_epochs): print(i, "start") pred = self.forward(x) loss = criterion(pred, y) optimizer.zero_grad() loss.backward() optimizer.step() closs = loss.item() print(i, closs, "\n=============") return closs
def predict(self, x): x = to_torch(x) out = None with no_grad(): out = self.forward(x).numpy() return out