Tensorflow API for the Dart programming language.
The revised goal of this project to provide an API for running pre-built Tensorflow models/graphs, typically built in another language, like Python.
Installation
This library uses native bindings, which (currently) are not easily distributed using Dart's Pub package manager.
Building the bindings, however, is easy and quick, as the build uses pre-built Tensorflow binaries, rather than re-building per user.
The Pub package does include pre-built libraries, but your mileage may vary.
It may make sense in your case to run the CMake build from within your ~/.pub-cache
locally.
Prerequisites
You'll need the following installed to run Tensorflow for Dart:
Building as a Dependency
If you are using package:tensorflow
as a dependency in a Dart project, then
you may be able to get by with the pre-built dynamic libraries in the lib/
folder. If not, you will have to manually enter the ~/.pub-cache
and run the
build.
Building by Itself
If you are contributing to the project, you will certainly need to be able to
build the project on the fly. Use the provided CMakeLists.txt
script to build
the project on-the-fly.
Basic Usage
Importing Graphs
This project supports loading and restoring models saved from other Tensorflow frontends, i.e. Python:
import 'package:tensorflow/tensorflow.dart' as tf;
void main() {
// Using the `SavedModel` API:
var model = SavedModelBundle('example/saved_models');
model.restore('variables.index');
// Or, you can import from a `GraphDef` protocol buffer:
var graph = Graph.fromGraphDef(graphDef);
graph['output'].run(feed: {'input': Tensor.from('Hello, world!')});
}
Low Level API
package:tensorflow/tensorflow.dart
theoretically supports the entire low-level
Tensorflow API. This can be used to perform a variety of complex mathematical
operations, and also be used to compose higher-level functionality. These bindings
are, for the most part, auto-generated, and there is not much incentive to properly
unit test them, considering that the most common use of this package will be to
execute existing Tensorflow models from other languages.
import 'package:tensorflow/tensorflow.dart' as tf;
void main() {
var shape = tf.Shape(6, 6);
var x = tf.getVariable(
'x',
shape: shape,
initializer: tf.randomUniform(
tf.constant(shape),
dtype: tf.DataType.DT_FLOAT,
),
);
x = tf.matMul(x, x);
print(x.run());
}