Code Monkey home page Code Monkey logo

Comments (5)

adamcrume avatar adamcrume commented on September 28, 2024

I agree. For comparison, here's the same code translated into code for the tensorflow crate:

extern crate random;
extern crate tensorflow;

use random::Source;
use std::fs::File;
use std::io::Read;
use tensorflow::{Buffer, SessionOptions, Session, Tensor, Step};

fn main() {
  let (w, b, n, steps) = (0.1, 0.3, 100, 201);
  let mut source = random::default();
  let mut x = Buffer::new(n);
  let mut y = Buffer::new(n);
  for i in 0..n {
    x[i] = (2.0 * source.read::<f64>() - 1.0) as f32;
    y[i] = w * x[i] + b;
  }

  let graph = "regression.pb"; // y = w * x + b                                                                                                                                               
  let mut session = Session::new(&SessionOptions::new()).unwrap();
  let mut proto = Vec::new();
  File::open(graph).unwrap().read_to_end(&mut proto).unwrap();
  session.extend_graph(&proto).unwrap();

  let x_tensor = Tensor::new_with_buffer(&[n as u64], x).unwrap();
  let y_tensor = Tensor::new_with_buffer(&[n as u64], y).unwrap();
  let mut init_step = Step::new();
  init_step.add_input("x", &x_tensor).unwrap();
  init_step.add_input("y", &y_tensor).unwrap();
  init_step.add_target("init").unwrap();
  session.run(&mut init_step).unwrap();

  let mut train_step = Step::new();
  train_step.add_input("x", &x_tensor).unwrap();
  train_step.add_input("y", &y_tensor).unwrap();
  train_step.add_target("train").unwrap();
  for _ in 0..steps {
    session.run(&mut train_step).unwrap();
  }

  let mut output_step = Step::new();
  let w_ix = output_step.request_output("w").unwrap();
  let b_ix = output_step.request_output("b").unwrap();
  session.run(&mut output_step).unwrap();

  let w_hat: f32 = output_step.take_output(w_ix).unwrap().data()[0];
  let b_hat: f32 = output_step.take_output(b_ix).unwrap().data()[0];

  assert!((w_hat - w).abs() < 1e-3);
  assert!((b_hat - b).abs() < 1e-3);
}

(I'll add this as an example after I get the Python code straightened out.)

from rust.

IvanUkhov avatar IvanUkhov commented on September 28, 2024

The main difference is in how run’s arguments are treated: tensorflow introduces the notion of step, which hides the arguments, while tensorflux spells them out. The motivation behind the latter is that it’s very explicit: easy to see what goes in and what goes out. It’s also make it flexible: the arguments are usable and reusable in any combination without constructing or querying auxiliary objects (add_input, add_target, request_output, and take_output). Perhaps it’s possible to pass even bare tensors, but Input, Output, and Target allow one to avoid repetitive memory allocations related to the conversion of strings from/into Rust into/from C.

There are also a couple of minor differences. The first is in the interfaces of Tensor. In the case of tensorflux, there is no need in data() or data_mut() as it dereferences to the underlaying data. The second is in the usage of u64 for representing natural number; usize is a more idiomatic way of doing this. These minor issues, however, can be easily fixed and, hence, are not a problem.

from rust.

adamcrume avatar adamcrume commented on September 28, 2024

For the signature to Session::run, I opened an RFC: #14.

I think the data in a Tensor could be accessed directly, although data() and data_mut() might still be useful. It's a minor benefit, but there's no harm.

As for the numeric type for the dimension sizes, we probably want to talk to the main TensorFlow project. For one, they use long long and not size_t. For another, I'm leery of using platform-specific data types, because a computation may span multiple machines (https://www.tensorflow.org/versions/r0.9/how_tos/distributed/index.html), which at least conceivably may have different architectures. (Whether that's actually supported or not, I don't know.) They may also not want to limit tensors to addressable memory.

from rust.

IvanUkhov avatar IvanUkhov commented on September 28, 2024

Sure, do what you think is best for the library. Please let me know if you consider any part of tensorflux valuable and would like it to appear in tensorflow, or if you need any help with the implementation in general. I’ll be happy to contribute. Thanks!

from rust.

adamcrume avatar adamcrume commented on September 28, 2024

I'm posting this a few days late, but the TensorFlow folks said that size_t is the correct type.

from rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.