Methods
(static) dispose(params, args, data) → {void}
Disposes the model from memory in the worker.
Parameters:
| Name | Type | Description |
|---|---|---|
params |
Object | Not used by this function. |
args |
Object | Not used by this function. |
data |
Object | Not used by this function. |
- Source:
Returns:
- Type
- void
Example
model.dispose();
(async, static) predict(options) → {Promise.<Array>}
Generate predictions from trained model Supports auto-reshaping for LSTM inputs Returns predictions for hydrological forecasting or classification
Parameters:
| Name | Type | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object | Prediction options Properties
|
- Source:
Returns:
Model predictions
- Type
- Promise.<Array>
Examples
// Dense model: Predict streamflow from current conditions
const model = await hydro.analyze.nn.createModel({
params: { type: 'dense' },
args: { inputShape: [3], units: [16, 1] }
});
await model.train({ params: { epochs: 50 }, data: [X_train, y_train] });
// Predict for new conditions
const newConditions = [[6, 22, 14]]; // [precip, temp, humidity]
const flowPrediction = await model.predict({ data: newConditions });
console.log(`Predicted flow: ${flowPrediction[0][0].toFixed(2)} m³/s`);
// LSTM: Forecast next day flow (auto-reshaping)
const lstmModel = await hydro.analyze.nn.createModel({
params: { type: 'lstm' },
args: { timeSteps: 7, features: 1, units: [32], outputUnits: 1 }
});
await lstmModel.train({ params: { epochs: 50 }, data: [sequences, targets] });
// 1D input automatically reshaped to [1, 7, 1] for LSTM
const last7Days = [100, 105, 98, 110, 115, 108, 112];
const nextDayPrediction = await lstmModel.predict({ data: last7Days });
console.log(`Tomorrow's flow: ${nextDayPrediction[0][0].toFixed(2)} m³/s`);
// Batch predictions: Multiple scenarios
const scenarios = [
[5, 20, 15], // Scenario 1: light rain
[15, 22, 18], // Scenario 2: heavy rain
[2, 25, 12] // Scenario 3: minimal rain
];
const predictions = await model.predict({ data: scenarios });
scenarios.forEach((scenario, i) => {
console.log(`Scenario ${i+1}: ${predictions[i][0].toFixed(2)} m³/s`);
});
// Classification: Flood risk category
const classifier = await hydro.analyze.nn.createModel({
params: { type: 'dense' },
args: { inputShape: [4], units: [32, 16, 3], outputActivation: 'softmax' }
});
// Train with flood categories: 0=Normal, 1=Warning, 2=Danger
const currentConditions = [[120, 8, 25, 85]]; // [flow, precip, temp, saturation]
const probabilities = await classifier.predict({ data: currentConditions });
const category = probabilities[0].indexOf(Math.max(...probabilities[0]));
const categories = ['Normal', 'Warning', 'Danger'];
console.log(`Flood Risk: ${categories[category]} (${(probabilities[0][category]*100).toFixed(1)}%)`);
(static) save(params, args, data) → {Promise.<Object>}
Saves the model to the local file system (browser downloads).
Parameters:
| Name | Type | Description |
|---|---|---|
params |
Object | Contains: name (filename for the model). |
args |
Object | Not used by this function. |
data |
Object | Not used by this function. |
- Source:
Returns:
Save status.
- Type
- Promise.<Object>
Example
await model.save({
params: { name: 'my-trained-model' }
});
(async, static) train(options) → {Promise.<Object>}
Train the neural network model with hydrological data Supports multiple data formats and training configurations Training runs in Web Worker for non-blocking UI
Parameters:
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object | Training options Properties
|
- Source:
Returns:
Training history with loss and metrics
- Type
- Promise.<Object>
Examples
// Basic training: Streamflow prediction from rainfall
const model = await hydro.analyze.nn.createModel({
params: { type: 'dense' },
args: { inputShape: [3], units: [16, 8, 1] }
});
const inputs = [[5, 20, 15], [3, 18, 12], [8, 25, 18]]; // [precip, temp, humidity]
const outputs = [[120], [95], [145]]; // streamflow
const history = await model.train({
params: { epochs: 100, batchSize: 16 },
data: [inputs, outputs]
});
console.log(`Final loss: ${history.loss[history.loss.length - 1]}`);
// LSTM training: Time series forecasting
const lstmModel = await hydro.analyze.nn.createModel({
params: { type: 'lstm' },
args: { timeSteps: 7, features: 2, units: [32], outputUnits: 1 }
});
// Training data: 7 days of [rainfall, temperature] → next day flow
const sequences = [
[[[10,20],[12,22],[8,21],[15,23],[5,19],[7,18],[9,20]]], // Input sequence
[[125]] // Target: next day flow
];
await lstmModel.train({
params: { epochs: 50, validationSplit: 0.2 },
data: sequences
});
// Training with callbacks and monitoring
await model.train({
params: {
epochs: 100,
batchSize: 32,
onEpochEnd: (epoch, logs) => {
console.log(`Epoch ${epoch}: loss=${logs.loss.toFixed(4)}, val_loss=${logs.val_loss?.toFixed(4)}`);
// Early stopping logic could go here
}
},
data: [X_train, y_train]
});
// Object format (alternative to array format)
await model.train({
params: { epochs: 50 },
data: {
inputs: flowObservations,
outputs: nextDayFlow
}
});