| 75 | } |
| 76 | |
| 77 | fn read_labels(fname: &str) -> Result<Vec<u8>, &'static str> { |
| 78 | |
| 79 | let mut data = try!(GzipData::from_file(fname)); |
| 80 | |
| 81 | if try!(MnistDigits::read_u32(&mut data)) != 8 * 256 + 1 { |
| 82 | return Err("Invalid magic number."); |
| 83 | } |
| 84 | |
| 85 | let n = try!(MnistDigits::read_u32(&mut data)); |
| 86 | |
| 87 | let l: Vec<u8> = data.iter().cloned().collect(); |
| 88 | if l.len() != n as usize { |
| 89 | return Err("Invalid number of items."); |
| 90 | } |
| 91 | |
| 92 | if !l.iter().all(|&x| x <= 9) { |
| 93 | return Err("Found invalid values for labels."); |
| 94 | } |
| 95 | |
| 96 | Ok(l) |
| 97 | } |
| 98 | |
| 99 | fn read_examples(fname: &str) -> Result<Vec<u8>, &'static str> { |
| 100 | |