Search code examples
tensorflowrustimporterror

Tensorflow in Rust, unresolved import


I am trying to use tensorflow in rust. I have followed the instructions on the github page of the rust tensorflow project. My Cargo.toml file has

[dependencies]
tensorflow = "0.15.0"

in it. My processor is an Intel i7-8700 and I'm running Ubuntu 18.04, which ought to mean they download a prebuilt binary, as per the github page, and I don't need to worry about other prerequisites.

I cargo run, and cargo downloads and builds a lot of packages. Then I try to copy their xor.rs example. It starts with a bunch of imports. First a lot of use std:: which are fine. Then we get to the tensorflow imports, starting with

use tensorflow::ops;
use tensorflow::train::AdadeltaOptimizer;
use tensorflow::train::MinimizeOptions;
use tensorflow::train::Optimizer;

and I immediately run into import issues. Here is the top of the list:

error[E0432]: unresolved import `tensorflow::ops`
 --> src/main.rs:7:5
  |
7 | use tensorflow::ops;
  |     ^^^^^^^^^^^^^^^ no `ops` in the root

error[E0432]: unresolved import `tensorflow::train`
 --> src/main.rs:8:17
  |
8 | use tensorflow::train::AdadeltaOptimizer;
  |                 ^^^^^ could not find `train` in `tensorflow`

error[E0432]: unresolved import `tensorflow::train`
 --> src/main.rs:9:17
  |
9 | use tensorflow::train::MinimizeOptions;
  |                 ^^^^^ could not find `train` in `tensorflow`

error[E0432]: unresolved import `tensorflow::train`
  --> src/main.rs:10:17
   |
10 | use tensorflow::train::Optimizer;
   |                 ^^^^^ could not find `train` in `tensorflow`

But according to both tensorflow's src/lib.rs file in the github repository linked above, and the documentation, there really should be modules tensorflow::ops and tensorflow::train available.

How come my cargo can't find them?


Solution

  • Resolved in the comments. I had to edit the Cargo.toml file to read

    tensorflow = {version = "0.15.0", features = ["experimental_training"]}
    

    as experimental training is apparently a required feature for these modules.