From 7d5e808a6c5a8aad5f8cd1373754c2f133e7c719 Mon Sep 17 00:00:00 2001 From: "Dr. Kashif Rasul" Date: Sat, 2 Nov 2019 16:09:10 +0100 Subject: [PATCH] test for forward --- pts/modules/feature.py | 4 +- test/modules/test_feature.py | 128 +++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 2 deletions(-) diff --git a/pts/modules/feature.py b/pts/modules/feature.py index 4d21aba..530615e 100644 --- a/pts/modules/feature.py +++ b/pts/modules/feature.py @@ -71,11 +71,11 @@ class FeatureAssembler(nn.Module): def process_static_cat(self, feature: torch.Tensor) -> torch.Tensor: if self.embeddings['embed_static'] is not None: feature = self.embeddings['embed_static'](feature) - return feature.unsqueeze(1).expand(-1, self.T, -1) + return feature.unsqueeze(1).expand(-1, self.T, -1).float() def process_dynamic_cat(self, feature: torch.Tensor) -> torch.Tensor: if self.embeddings['embed_dynamic'] is None: - return feature + return feature.float() else: return self.embeddings['embed_dynamic'](feature) diff --git a/test/modules/test_feature.py b/test/modules/test_feature.py index 8ab5a9f..5417c71 100644 --- a/test/modules/test_feature.py +++ b/test/modules/test_feature.py @@ -4,6 +4,7 @@ from itertools import chain, combinations import torch import torch.nn as nn +from torch.distributions import Uniform from pts.modules import FeatureEmbedder, FeatureAssembler @@ -139,4 +140,131 @@ def test_feature_assembler(config): act_params_len = len([p for p in assemble_feature.parameters()]) assert exp_params_len == act_params_len + def test_forward_pass(): + N, T = config["N"], config["T"] + + inp_features = [] + out_features = [] + + if "static_cat" not in enabled_features: + inp_features.append(torch.zeros((N, 1))) + out_features.append(torch.zeros((N, T, 1))) + elif embed_static: # and 'static_cat' in enabled_features + C = config["static_cat"]["C"] + inp_features.append( + torch.cat( + [ + torch.randint( + 0, + config["embed_static"]["cardinalities"][c], + (N, 1), + ) + for c in range(C) + ], + dim=1, + ) + ) + out_features.append( + torch.ones( + ( + N, + T, + sum(config["embed_static"]["embedding_dims"]), + ) + ) + ) + else: # not embed_static and 'static_cat' in enabled_features + C = config["static_cat"]["C"] + inp_features.append( + torch.cat( + [ + torch.randint( + 0, + config["embed_static"]["cardinalities"][c], + (N, 1), + ) + for c in range(C) + ], + dim=1, + ) + ) + out_features.append( + inp_features[-1].unsqueeze(1).expand(-1, T, -1).float() + ) + + if "static_real" not in enabled_features: + inp_features.append(torch.zeros((N, 1))) + out_features.append(torch.zeros((N, T, 1))) + else: + C = config["static_real"]["C"] + static_real = torch.empty((N,C)).uniform_(0,100) + inp_features.append(static_real) + out_features.append( + static_real.unsqueeze(-2).expand(-1, T, -1) + ) + + if "dynamic_cat" not in enabled_features: + inp_features.append(torch.zeros((N, T, 1))) + out_features.append(torch.zeros((N, T, 1))) + elif embed_dynamic: # and 'static_cat' in enabled_features + C = config["dynamic_cat"]["C"] + inp_features.append( + torch.cat( + [ + torch.randint( + 0, + config["embed_dynamic"]["cardinalities"][ + c + ], + (N, T, 1), + ) + for c in range(C) + ], + dim=2, + ) + ) + out_features.append( + torch.ones( + ( + N, + T, + sum(config["embed_dynamic"]["embedding_dims"]), + ) + ) + ) + else: # not embed_dynamic and 'dynamic_cat' in enabled_features + C = config["dynamic_cat"]["C"] + inp_features.append( + torch.cat( + [ + torch.randint( + 0, + config["embed_dynamic"]["cardinalities"][ + c + ], + (N, T, 1), + ) + for c in range(C) + ], + dim=2, + ) + ) + out_features.append(inp_features[-1].float()) + + if "dynamic_real" not in enabled_features: + inp_features.append(torch.zeros((N, T, 1))) + out_features.append(torch.zeros((N, T, 1))) + else: + C = config["dynamic_real"]["C"] + dynamic_real = torch.empty((N, T, C)).uniform_(0,100) + inp_features.append(dynamic_real) + out_features.append(dynamic_real) + + act_output = assemble_feature(*inp_features) + exp_output = torch.cat(out_features, dim=2) + + assert exp_output.shape == act_output.shape + assert torch.sum(exp_output - act_output) < 1e-20 + test_parameters_length() + test_forward_pass()