Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need of documentation and example on how to use the neural network after training #1

Open
AurelienPls opened this issue Feb 13, 2025 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@AurelienPls
Copy link

I'm using nnbma to emulate an astrophysics code.

After training the network with a dataset of inputs X_train and associated outputs Y_train, I want to evaluate the neural network on a new input X_new for a subset of outputs, and plot the result.

To do this, I need to:

  • Normalize X_new in the same way as the original training data.
  • Restrict the evaluation of the neural network to a subset of outputs.

However, I am unable to locate the appropriate methods within the provided classes to accomplish this. Would it be possible to provide documentation or an example notebook demonstrating how to plot and extract output data from nnbma using the intended methods?

@pierrePalud pierrePalud added the documentation Improvements or additions to documentation label Feb 13, 2025
@pierrePalud pierrePalud self-assigned this Feb 13, 2025
@pierrePalud
Copy link
Collaborator

Hello ! Thanks for your feedback. We will add an example notebook in the documentation as you suggested to illustrate how to use a trained network in practice.

In the meantime, here are some answers to address your difficulties:

To normalize your new input
Before training, you defined a normalization for your data. After training, it is essential that you use the exact same normalization. We implemented some data pre-processing (including normalization) in nnbma.operators, but you might prefer to resort to sklearn.preprocessing.StandardScaler. In this case, save your scaler object (for instance, in a pickle file) after training to be able to load it for future uses.

For instance, let's assume that your network net is a FullyConnected instance, and that it has two inputs and 10 outputs called "output 0" to "output 9". Say that after training, you

  • saved a StandardScaler object in a scaler.pickle file,
  • and saved your trained network in a folder path_to_network, using the class method save.

To use correctly your network, here are the 4 steps you should follow :

# step 1: load your scaler, already fitted before training
import pickle

with open("scaler.pickle", "rb") as f:
  scaler = pickle.load(f)

# step 2: load your trained network 
net = FullyConnected.load(path_to_network)

# step 3: define your new input and scale it
X_new = np.array([[1., 2.]]) # creates one input of shape (1, 2)
X_new_scaled = scaler.transform(X_new)

# step 4: evaluate your network on X_new_scaled 
pred_full = net(X_new_scaled) # evaluates `net` on all 10 outputs. pred_full has shape (1, 10).

To restrict the evaluation of the neural network to a subset of outputs:
We implemented a RestrictableLinear class (innnbma.layers.restrictable_layer.py). This class defines a linear transformation with outputs that have names. By default for the FullyConnected and DenselyConnected classes, the output layer is an instance of RestrictableLayer. Each of these two network classes has a restrict_to_output_subset() method that does exactly what you want.

For instance, continuing the example above with 10 outputs called "output 0" to "output 9".

net.restrict_to_output_subset(["output 1", "output 4", "output 7"]) # the network now only predicts these 3 outputs

pred_restricted = net(X_new_scaled) # pred_restricted has shape (1, 3)

For more information on this feature of nnbma, see the associated notebook in the documentation Gallery of examples : https://ism-model-nn-approximation.readthedocs.io/en/latest/restrictable-layer.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants