1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-18 01:44:34 +08:00

Add torch.no_grad for evaluation

This commit is contained in:
D-X-Y
2021-03-12 02:46:04 +00:00
parent 67fbdafe76
commit db59713d36
5 changed files with 35 additions and 37 deletions

View File

@@ -208,6 +208,7 @@ class ALSTM(Model):
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float().to(self.device) feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float().to(self.device) label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float().to(self.device)
with torch.no_grad():
pred = self.ALSTM_model(feature) pred = self.ALSTM_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
losses.append(loss.item()) losses.append(loss.item())
@@ -295,10 +296,7 @@ class ALSTM(Model):
x_batch = torch.from_numpy(x_values[begin:end]).float().to(self.device) x_batch = torch.from_numpy(x_values[begin:end]).float().to(self.device)
with torch.no_grad(): with torch.no_grad():
if self.use_gpu:
pred = self.ALSTM_model(x_batch).detach().cpu().numpy() pred = self.ALSTM_model(x_batch).detach().cpu().numpy()
else:
pred = self.ALSTM_model(x_batch).detach().numpy()
preds.append(pred) preds.append(pred)

View File

@@ -195,6 +195,7 @@ class ALSTM(Model):
# feature[torch.isnan(feature)] = 0 # feature[torch.isnan(feature)] = 0
label = data[:, -1, -1].to(self.device) label = data[:, -1, -1].to(self.device)
with torch.no_grad():
pred = self.ALSTM_model(feature.float()) pred = self.ALSTM_model(feature.float())
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
losses.append(loss.item()) losses.append(loss.item())

View File

@@ -208,6 +208,7 @@ class GRU(Model):
feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float().to(self.device) feature = torch.from_numpy(x_values[indices[i : i + self.batch_size]]).float().to(self.device)
label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float().to(self.device) label = torch.from_numpy(y_values[indices[i : i + self.batch_size]]).float().to(self.device)
with torch.no_grad():
pred = self.gru_model(feature) pred = self.gru_model(feature)
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
losses.append(loss.item()) losses.append(loss.item())

View File

@@ -195,6 +195,7 @@ class GRU(Model):
# feature[torch.isnan(feature)] = 0 # feature[torch.isnan(feature)] = 0
label = data[:, -1, -1].to(self.device) label = data[:, -1, -1].to(self.device)
with torch.no_grad():
pred = self.GRU_model(feature.float()) pred = self.GRU_model(feature.float())
loss = self.loss_fn(pred, label) loss = self.loss_fn(pred, label)
losses.append(loss.item()) losses.append(loss.item())
@@ -280,10 +281,7 @@ class GRU(Model):
feature = data[:, :, 0:-1].to(self.device) feature = data[:, :, 0:-1].to(self.device)
with torch.no_grad(): with torch.no_grad():
if self.use_gpu:
pred = self.GRU_model(feature.float()).detach().cpu().numpy() pred = self.GRU_model(feature.float()).detach().cpu().numpy()
else:
pred = self.GRU_model(feature.float()).detach().numpy()
preds.append(pred) preds.append(pred)