use anyhow::Result;
use burn::module::Module;
use burn::tensor::backend::Backend;
use burn::record::{BinFileRecorder, FullPrecisionSettings};
use std::path::Path;
use super::step_3_cnn_lstm_model_arch::TimeSeriesCnnLstm;
pub fn save_model<B: Backend>(
model: &TimeSeriesCnnLstm<B>,
path: &Path,
) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let path_str = path.to_str().ok_or_else(|| anyhow::anyhow!("Invalid path"))?;
let recorder = BinFileRecorder::<FullPrecisionSettings>::new();
model.clone().save_file(path_str, &recorder)?;
println!("Model saved to: {}", path_str);
Ok(())
}
pub fn load_model<B: Backend>(
path: &Path,
device: &B::Device,
) -> Result<TimeSeriesCnnLstm<B>> {
let path_str = path.to_str().ok_or_else(|| anyhow::anyhow!("Invalid path"))?;
let recorder = BinFileRecorder::<FullPrecisionSettings>::new();
let base_model = TimeSeriesCnnLstm::<B>::new(
7, 32, 1, 1, false, 0.2, device,
);
let model = base_model.load_file(path_str, &recorder, device)?;
println!("Lightweight model loaded from: {}", path_str);
Ok(model)
}