Skip to main content

NN

The NN component contains functions for creating a feedforward neural network that predicts data from N number of inputs to M number of outputs. It is powered by the TensorFlow.js library, which aids in development through its straightforward interface.

If the NN component is not available, we can create a HydroLang instance and extract the neural network component from the analyze module for further usage:

const hydrolang = new Hydrolang();
const { nn } = hydrolang.analyze;

Exercise 1

We can now create and train a model that accepts 50 inputs and returns 100 outputs by passing the data through 50 different neurons that map the data accordingly:

const model = nn.createModel({
params: { inputs: 50, neurons: 50, outputs: 100 }});
Tip

Note that we can alter the number of neurons for training purposes, but more does not necessarily mean better. This would be useful for spatio-temporal down and upscaling procedures, such as rainfall disaggregation.

Next, we can feed the training data using available methods to generate random data within the component and shape it into a machine learning system structure:

const { inputs, outputs, outputMin, outputMax } = nn.convertToTensor({
data: {
inputSet: stats.generateRandomData(50),
outputSet: stats.generateRandomData(100)}});

Using JS destructuring, we can obtain the variables for inputs and outputs by declaring them in the format: {} = functionCall(). We're now ready to train the model by calling the following method:

await nn.trainModel({ 
params: { model },
data: { inputs, outputs }});
Tip

Experiment with different values for the optional args parameter, such as adjusting the number of epochs, batch size, and validation split to optimize model performance. Additionally, setting the learning rate for the optimizer can also have a significant impact on model training.

On the screen, we'll see a graph showing fitting parameters, including losses and mean square error. We can use these graphic tools to evaluate whether the parameters we trained our model with are adequate or whether we need to adjust them.

Finally, we can use our trained model to evaluate the model input/outputs by using the following code:

const pred = nn.prediction({
params: { model },
args: { outputMin, outputMax },
data: nn.generateRandomData(50)});
Note

You can also save the model for further usage into your local machine using the following:

nn.saveModel({
params: { model }
})
const hydrolang = new Hydrolang();
const {hydro, stats, nn} = hydrolang.analyze;
const main = async () => {
  //Paste code here
};
main();

Tip

More info about the neural networks component in the documentation page