|
| 1 | +import unittest, torch |
| 2 | +from scimlstudio.models import FeedForwardNeuralNetwork |
| 3 | +from scimlstudio.utils import evaluate_scalar, Standardize |
| 4 | + |
| 5 | +device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
| 6 | +dtype = torch.float32 |
| 7 | +args = { |
| 8 | + "device": device, |
| 9 | + "dtype": dtype |
| 10 | +} |
| 11 | + |
| 12 | +class TestFeedForwardNeuralNetwork(unittest.TestCase): |
| 13 | + """ |
| 14 | + Class defining the test cases for the feed forward neural network model |
| 15 | + """ |
| 16 | + |
| 17 | + def test_nn_model_1d(self): |
| 18 | + |
| 19 | + # training data |
| 20 | + xtrain = torch.linspace(0, 2*torch.pi, 7, **args).reshape(-1,1) |
| 21 | + ytrain = torch.sin(xtrain) |
| 22 | + |
| 23 | + # testing data |
| 24 | + xtest = torch.linspace(0, 2*torch.pi, 100, **args).reshape(-1,1) |
| 25 | + ytest = torch.sin(xtest) |
| 26 | + |
| 27 | + # network |
| 28 | + network = torch.nn.Sequential( |
| 29 | + torch.nn.Linear(in_features=xtrain.shape[1], out_features=32), |
| 30 | + torch.nn.GELU(), |
| 31 | + torch.nn.Linear(in_features=32, out_features=32), |
| 32 | + torch.nn.GELU(), |
| 33 | + torch.nn.Linear(in_features=32, out_features=32), |
| 34 | + torch.nn.GELU(), |
| 35 | + torch.nn.Linear(in_features=32, out_features=ytrain.shape[1]), |
| 36 | + ).to(**args) |
| 37 | + |
| 38 | + def init_weights(m): |
| 39 | + """ |
| 40 | + Function for initializing the weights using glorot (or xavier) initialization |
| 41 | + """ |
| 42 | + |
| 43 | + if isinstance(m, torch.nn.Linear): |
| 44 | + torch.nn.init.xavier_normal_(m.weight) |
| 45 | + m.bias.data.fill_(0.0) |
| 46 | + |
| 47 | + # initial weights |
| 48 | + network.apply(init_weights) |
| 49 | + |
| 50 | + # data transforms |
| 51 | + input_transform = Standardize(xtrain) |
| 52 | + output_transform = Standardize(ytrain) |
| 53 | + |
| 54 | + # create model instance |
| 55 | + model = FeedForwardNeuralNetwork(xtrain, ytrain, network, input_transform=input_transform, output_transform=output_transform) |
| 56 | + |
| 57 | + # optimizer |
| 58 | + optimizer = torch.optim.Adam(model.parameters, lr=0.01) |
| 59 | + |
| 60 | + # loss function |
| 61 | + loss_func = torch.nn.MSELoss() |
| 62 | + |
| 63 | + # fit the model |
| 64 | + model.fit(optimizer, loss_func, batch_size=xtrain.shape[0], epochs=100) |
| 65 | + |
| 66 | + # predict |
| 67 | + ytest_pred = model.predict(xtest) |
| 68 | + |
| 69 | + # metrics |
| 70 | + r2 = evaluate_scalar(ytest.reshape(-1,), ytest_pred.reshape(-1,), "r2") |
| 71 | + nrmse = evaluate_scalar(ytest.reshape(-1,), ytest_pred.reshape(-1,), "nrmse") |
| 72 | + |
| 73 | + assert nrmse < 2e-2 and r2 > 0.99 |
| 74 | + |
| 75 | + def test_nn_model_2d(self): |
| 76 | + |
| 77 | + # train |
| 78 | + x1 = torch.linspace(0,1,5,**args) |
| 79 | + x2 = torch.linspace(0,1,5,**args) |
| 80 | + X1, X2 = torch.meshgrid(x1, x2, indexing="ij") |
| 81 | + xtrain = torch.hstack(( X1.reshape(-1,1), X2.reshape(-1,1) )) |
| 82 | + ytrain = torch.cos(torch.sum(xtrain, axis=1))*torch.exp(torch.prod(xtrain, axis=1)) |
| 83 | + ytrain = ytrain.reshape(-1,1) |
| 84 | + |
| 85 | + # test |
| 86 | + x1 = torch.linspace(0,1,15,**args) |
| 87 | + x2 = torch.linspace(0,1,15,**args) |
| 88 | + X1, X2 = torch.meshgrid(x1, x2, indexing="ij") |
| 89 | + xtest = torch.hstack(( X1.reshape(-1,1), X2.reshape(-1,1) )) |
| 90 | + ytest = torch.cos(xtest[:,0]+xtest[:,1])*torch.exp(xtest[:,0]*xtest[:,1]) |
| 91 | + ytest = ytest.reshape(-1,1) |
| 92 | + |
| 93 | + # network |
| 94 | + network = torch.nn.Sequential( |
| 95 | + torch.nn.Linear(in_features=xtrain.shape[1], out_features=32), |
| 96 | + torch.nn.GELU(), |
| 97 | + torch.nn.Linear(in_features=32, out_features=32), |
| 98 | + torch.nn.GELU(), |
| 99 | + torch.nn.Linear(in_features=32, out_features=32), |
| 100 | + torch.nn.GELU(), |
| 101 | + torch.nn.Linear(in_features=32, out_features=ytrain.shape[1]), |
| 102 | + ).to(**args) |
| 103 | + |
| 104 | + def init_weights(m): |
| 105 | + """ |
| 106 | + Function for initializing the weights using glorot (or xavier) initialization |
| 107 | + """ |
| 108 | + |
| 109 | + if isinstance(m, torch.nn.Linear): |
| 110 | + torch.nn.init.xavier_normal_(m.weight) |
| 111 | + m.bias.data.fill_(0.0) |
| 112 | + |
| 113 | + # initial weights |
| 114 | + network.apply(init_weights) |
| 115 | + |
| 116 | + # data transforms |
| 117 | + input_transform = Standardize(xtrain) |
| 118 | + output_transform = Standardize(ytrain) |
| 119 | + |
| 120 | + # create model instance |
| 121 | + model = FeedForwardNeuralNetwork(xtrain, ytrain, network, input_transform=input_transform, output_transform=output_transform) |
| 122 | + |
| 123 | + # optimizer |
| 124 | + optimizer = torch.optim.Adam(model.parameters, lr=0.01) |
| 125 | + |
| 126 | + # loss function |
| 127 | + loss_func = torch.nn.MSELoss() |
| 128 | + |
| 129 | + # fit the model |
| 130 | + model.fit(optimizer, loss_func, batch_size=xtrain.shape[0], epochs=100) |
| 131 | + |
| 132 | + # predict |
| 133 | + ytest_pred = model.predict(xtest) |
| 134 | + |
| 135 | + # metrics |
| 136 | + r2 = evaluate_scalar(ytest.reshape(-1,), ytest_pred.reshape(-1,), "r2") |
| 137 | + nrmse = evaluate_scalar(ytest.reshape(-1,), ytest_pred.reshape(-1,), "nrmse") |
| 138 | + |
| 139 | + assert nrmse < 1e-2 and r2 > 0.99 |
| 140 | + |
| 141 | + def test_input_output_shapes(self): |
| 142 | + |
| 143 | + # dummy training data |
| 144 | + xtrain = torch.rand(10, 5, **args) |
| 145 | + ytrain = torch.rand(10, 1, **args) |
| 146 | + |
| 147 | + # network |
| 148 | + network = torch.nn.Sequential( |
| 149 | + torch.nn.Linear(in_features=xtrain.shape[1], out_features=16), |
| 150 | + torch.nn.Tanh(), |
| 151 | + torch.nn.Linear(in_features=16, out_features=16), |
| 152 | + torch.nn.Tanh(), |
| 153 | + torch.nn.Linear(in_features=16, out_features=ytrain.shape[1]), |
| 154 | + ).to(**args) |
| 155 | + |
| 156 | + # create model instance |
| 157 | + model = FeedForwardNeuralNetwork(xtrain, ytrain, network) |
| 158 | + |
| 159 | + # optimizer |
| 160 | + optimizer = torch.optim.Adam(model.parameters, lr=0.01) |
| 161 | + |
| 162 | + # loss function |
| 163 | + loss_func = torch.nn.MSELoss() |
| 164 | + |
| 165 | + # fit the model |
| 166 | + model.fit(optimizer, loss_func, batch_size=xtrain.shape[0], epochs=100) |
| 167 | + |
| 168 | + # predict - 1 samples |
| 169 | + ypred = model.predict(xtrain[0]) |
| 170 | + assert ypred.ndim == 1 and ypred.shape[0] == 1 |
| 171 | + |
| 172 | + # predict - 5 samples |
| 173 | + ypred = model.predict(xtrain[:5]) |
| 174 | + assert ypred.ndim == 2 and ypred.shape[0] == 5 |
| 175 | + |
| 176 | +if __name__ == '__main__': |
| 177 | + unittest.main() |
0 commit comments