| 97 | } |
| 98 | |
| 99 | fn read_examples(fname: &str) -> Result<Vec<u8>, &'static str> { |
| 100 | |
| 101 | let mut data = try!(GzipData::from_file(fname)); |
| 102 | |
| 103 | if try!(MnistDigits::read_u32(&mut data)) != 8 * 256 + 3 { |
| 104 | return Err("Invalid magic number."); |
| 105 | } |
| 106 | |
| 107 | let n = try!(MnistDigits::read_u32(&mut data)); |
| 108 | |
| 109 | let rows = try!(MnistDigits::read_u32(&mut data)); |
| 110 | let cols = try!(MnistDigits::read_u32(&mut data)); |
| 111 | if rows != 28 || cols != 28 { |
| 112 | return Err("Invalid number of rows or columns."); |
| 113 | } |
| 114 | |
| 115 | let v = data.buf(); |
| 116 | if v.len() != (n * 28 * 28) as usize { |
| 117 | return Err("Could not read data."); |
| 118 | } |
| 119 | |
| 120 | Ok(v.to_vec()) |
| 121 | } |
| 122 | |
| 123 | pub fn from(vectors_fname: &str, labels_fname: &str) -> Result<(Matrix<u8>, Vec<u8>), &'static str> { |
| 124 | |