|
| 1 | +#![cfg_attr(feature="nightly", feature(alloc_system))] |
| 2 | +#[cfg(feature="nightly")] |
| 3 | +extern crate alloc_system; |
| 4 | +extern crate random; |
| 5 | +extern crate tensorflow; |
| 6 | + |
| 7 | +use random::Source; |
| 8 | +use std::error::Error; |
| 9 | +use std::fs::File; |
| 10 | +use std::io::Read; |
| 11 | +use std::result::Result; |
| 12 | +use std::path::Path; |
| 13 | +use std::process::exit; |
| 14 | +use tensorflow::Code; |
| 15 | +use tensorflow::Graph; |
| 16 | +use tensorflow::ImportGraphDefOptions; |
| 17 | +use tensorflow::Session; |
| 18 | +use tensorflow::SessionOptions; |
| 19 | +use tensorflow::Status; |
| 20 | +use tensorflow::StepWithGraph; |
| 21 | +use tensorflow::Tensor; |
| 22 | + |
| 23 | +fn main() { |
| 24 | + // Putting the main code in another function serves two purposes: |
| 25 | + // 1. We can use the `?` operator. |
| 26 | + // 2. We can call exit safely, which does not run any destructors. |
| 27 | + exit(match run() { |
| 28 | + Ok(_) => 0, |
| 29 | + Err(e) => { |
| 30 | + println!("{}", e); |
| 31 | + 1 |
| 32 | + } |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +fn run() -> Result<(), Box<Error>> { |
| 37 | + let filename = "examples/regression_checkpoint/model.pb"; // y = w * x + b |
| 38 | + if !Path::new(filename).exists() { |
| 39 | + return Err(Box::new(Status::new_set(Code::NotFound, |
| 40 | + &format!("Run 'python regression_checkpoint.py' to generate \ |
| 41 | + {} and try again.", |
| 42 | + filename)) |
| 43 | + .unwrap())); |
| 44 | + } |
| 45 | + |
| 46 | + // Generate some test data. |
| 47 | + let w = 0.1; |
| 48 | + let b = 0.3; |
| 49 | + let num_points = 100; |
| 50 | + let steps = 201; |
| 51 | + let mut rand = random::default(); |
| 52 | + let mut x = Tensor::new(&[num_points as u64]); |
| 53 | + let mut y = Tensor::new(&[num_points as u64]); |
| 54 | + for i in 0..num_points { |
| 55 | + x[i] = (2.0 * rand.read::<f64>() - 1.0) as f32; |
| 56 | + y[i] = w * x[i] + b; |
| 57 | + } |
| 58 | + |
| 59 | + // Load the computation graph defined by regression.py. |
| 60 | + let mut graph = Graph::new(); |
| 61 | + let mut proto = Vec::new(); |
| 62 | + File::open(filename)?.read_to_end(&mut proto)?; |
| 63 | + graph.import_graph_def(&proto, &ImportGraphDefOptions::new())?; |
| 64 | + let mut session = Session::new(&SessionOptions::new(), &graph)?; |
| 65 | + let op_x = graph.operation_by_name_required("x")?; |
| 66 | + let op_y = graph.operation_by_name_required("y")?; |
| 67 | + let op_init = graph.operation_by_name_required("init")?; |
| 68 | + let op_train = graph.operation_by_name_required("train")?; |
| 69 | + let op_w = graph.operation_by_name_required("w")?; |
| 70 | + let op_b = graph.operation_by_name_required("b")?; |
| 71 | + let op_file_path = graph.operation_by_name_required("save/Const")?; |
| 72 | + let op_save = graph.operation_by_name_required("save/control_dependency")?; |
| 73 | + let file_path_tensor: Tensor<String> = Tensor::from(String::from("examples/regression_checkpoint/saved.ckpt")); |
| 74 | + |
| 75 | + // Load the test data into the session. |
| 76 | + let mut init_step = StepWithGraph::new(); |
| 77 | + init_step.add_target(&op_init); |
| 78 | + session.run(&mut init_step)?; |
| 79 | + |
| 80 | + // Train the model. |
| 81 | + let mut train_step = StepWithGraph::new(); |
| 82 | + train_step.add_input(&op_x, 0, &x); |
| 83 | + train_step.add_input(&op_y, 0, &y); |
| 84 | + train_step.add_target(&op_train); |
| 85 | + for _ in 0..steps { |
| 86 | + session.run(&mut train_step)?; |
| 87 | + } |
| 88 | + |
| 89 | + // Save the model. |
| 90 | + let mut step = StepWithGraph::new(); |
| 91 | + step.add_input(&op_file_path, 0, &file_path_tensor); |
| 92 | + step.add_target(&op_save); |
| 93 | + session.run(&mut step)?; |
| 94 | + |
| 95 | + // Initialize variables, to erase trained data. |
| 96 | + session.run(&mut init_step)?; |
| 97 | + |
| 98 | + // Load the model. |
| 99 | + let op_load = graph.operation_by_name_required("save/restore_all")?; |
| 100 | + let mut step = StepWithGraph::new(); |
| 101 | + step.add_input(&op_file_path, 0, &file_path_tensor); |
| 102 | + step.add_target(&op_load); |
| 103 | + session.run(&mut step)?; |
| 104 | + |
| 105 | + // Grab the data out of the session. |
| 106 | + let mut output_step = StepWithGraph::new(); |
| 107 | + let w_ix = output_step.request_output(&op_w, 0); |
| 108 | + let b_ix = output_step.request_output(&op_b, 0); |
| 109 | + session.run(&mut output_step)?; |
| 110 | + |
| 111 | + // Check our results. |
| 112 | + let w_hat: f32 = output_step.take_output(w_ix)?[0]; |
| 113 | + let b_hat: f32 = output_step.take_output(b_ix)?[0]; |
| 114 | + println!("Checking w: expected {}, got {}. {}", |
| 115 | + w, |
| 116 | + w_hat, |
| 117 | + if (w - w_hat).abs() < 1e-3 { |
| 118 | + "Success!" |
| 119 | + } else { |
| 120 | + "FAIL" |
| 121 | + }); |
| 122 | + println!("Checking b: expected {}, got {}. {}", |
| 123 | + b, |
| 124 | + b_hat, |
| 125 | + if (b - b_hat).abs() < 1e-3 { |
| 126 | + "Success!" |
| 127 | + } else { |
| 128 | + "FAIL" |
| 129 | + }); |
| 130 | + Ok(()) |
| 131 | +} |
0 commit comments