Comments (5)
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.
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.
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.
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.
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)
- cant generate &[u8] slice for ConfigProto to SessionOptions::set_config() HOT 4
- There should be a way to obtain a variable from a saved model HOT 3
- GPU features does not work on Windows native (v0.20.0 with TF v2.11.0) HOT 4
- Codegen crate HOT 3
- Tensorflow-sys build.rs wrong version HOT 1
- Issue when building on M1 Mac HOT 1
- Rust model doesn't seem to produce the same output as python model HOT 6
- Reshape isn't the same as numpy HOT 1
- Do have any examples of object recognition? HOT 3
- ROCm support
- Incompatible with projects using protobuf 2.28 HOT 1
- Misaligned pointer exposure in `<String as TensorType>::unpack` HOT 1
- Tensorflow is taking over my openssl and causing segfaults HOT 5
- Is there an example of range related operation HOT 1
- How can I use tf.signal.stft in rust binding? HOT 1
- String tensor vs utf8 encoding HOT 4
- rusticl support?? HOT 2
- Segfault when using session.run with a variable on x86 Mac HOT 2
- `python examples/mobilenetv3/create_model.py` fails with `ValueError` HOT 1
- `examples/mobilenetv3.rs` fails with `{inner:0x60bb93e599a0, InvalidArgument: Input 'input_1' not found}` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.