Add support for 1D CNN layers #123
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What this PR solves
TorchModel's evaluate function expects a dictionary input, where each value is an input tensor of shape[n_batch, n_sample]or[n_samples]if batch size is 1.If a user wants to create a
TorchModelwith annn.Modulethat contains convolution layers or RNN layers for example, the expected input shape for that model will be different.This PR adds a method to check for different model architectures, and adjusts the input shape accordingly to correctly run the supported architectures.
This adjustment is needed for running the CoAD NN.
Implementation
Models with linear, or recurrent (RNN, LSTM, GRU) layers are currently supported. Models with 2D and 3D convolutional layers or Transformer layers are not supported at this time.
The
TorchModulenow has a method that will permute the input shape as needed to create a dictionary that follows theTorchModel'sevaluatefunction requirements, as well as reconstruct the output tensor from the output dict correctly to return the expected shape for each architecture. The expected shape depends on the model type and inherits from the originalnn.Module:[n_batch, n_samples, n_dim][n_batch, n_dim, n_samples][n_batch, n_samples, n_dim], or[n_samples, n_batch, n_dim]if the RNN'sbatch_firstattribute is set to False (it is by default).For
TorchModel, the input is still expected to be a dictionary input, where each value is an input tensor of shape[n_batch, n_sample]or[n_samples]if batch size is 1. When constructing the torch tensor before passing the input to the underlyingnn.Module, and after obtaining the tensor from the model output, there is now a method that permutes the tensor as needed to pass the correct shape based on the model type (list above).I also adjusted a bug in the
TorchModel._arrange_inputsmethod that was adding a dimension to the final tensor, which was causing issues when trying to permute dimensions.I did not implement any tests yet, but should at some point.