AI Stack Exchange
2024-09-16 13:24 UTC
Score 29.0
AI-110-20240916-social-media-4c137062
Full article
I'm just starting out in the world of machine learning, and I really like Rust. I've been testing and learning more. I took the example of transfer training and did some tests, but I can't understand why I have high accuracy in training and low accuracy in testing using the same validation base. Why? I studied overfit, but it doesn't seem to be the case, because I'm using the same validation base without new data. use std::env; use std::error::Error; use std::path::PathBuf; use anyhow::{ bail, Result }; use tch::nn::{ self, ModuleT, OptimizerConfig, VarStore }; use tch::vision::{ imagenet, resnet }; use tch::{ Device, Kind, Tensor }; pub fn bee_test() -> Result > { tch::manual_seed(123); let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; let project_dir = PathBuf::from(manifest_dir); let dataset_path = project_dir.join("data/hymenoptera_data"); let dataset = imagenet::load_from_dir(dataset_path)?; println!("{dataset:?}"); let model_path = project_dir.join("data/bee.ot"); println!("Caminho do modelo: {:?}", model_path); let device = Device::cuda_if_available(); let mut vs = VarStore::new(device); vs.load(model_path.as_path()).map_err(|op| { format!("Erro ao carregar o modelo: {:?}", op); op })?; let net = resnet::resnet34_no_final_layer(&vs.root()); let linear = nn::linear(vs.root(), 512, 2, Default::default()); let net2: nn::Sequential = nn ::seq() .add_fn(move |xs| net.forward_t(xs, false)) .add(linear); let predicted = net2.forward_t(&dataset.test_images, false); let prob…