saveModuleParameters function
Saves the parameters (weights) of a given Module to a JSON file.
The parameters are extracted as a list of double values from the
Module.parameters() method and then encoded as a JSON array.
module: The Module instance whose parameters are to be saved.
filePath: The path to the file where the weights will be saved.
Saves the parameters (weights) of a given Module to a JSON file.
The parameters are extracted as a list of double values from the
Module.parameters() method and then encoded as a JSON array.
module: The Module instance whose parameters are to be saved.
filePath: The path to the file where the weights will be saved.
Implementation
Future<void> saveModuleParameters(Module module, String filePath) async {
final List<Value> parameters = module.parameters();
final List<double> weightsData = parameters.map((v) => v.data).toList();
final String jsonString = jsonEncode(weightsData);
final File file = File(filePath);
try {
await file.writeAsString(jsonString);
print('Weights successfully saved to: $filePath');
} catch (e) {
print('Error saving weights to $filePath: $e');
}
}