Struct autograd::tensor::Tensor [−][src]
pub struct Tensor<'graph, F: Float> { /* fields omitted */ }
Expand description
Lazy N-dimensional array.
Tensor
is:
- created by operations of a
Graph
. - not evaluated until
Tensor::eval
,Graph::eval
orEval::run
is called. - cheap to
Copy
since it contains only refs to the owned internal objects.
The builtin operations for tensors are provided as Graph’s methods.
use autograd as ag;
ag::with(|graph| { // `Graph` is necessary to create tensors.
// `random` is just a symbolic object belongs to `graph`.
let random: ag::Tensor<f64> = graph.standard_normal(&[2, 3]);
// This is ok since tensor's binary operators are overloaded!
let mul = random * 3.;
// Evaluates the symbolic tensor as an ndarray::Array<T, IxDyn>.
type NdArray = ag::NdArray<f64>;
let mul_val: Result<NdArray, ag::EvalError> = mul.eval(&[]);
// Reshapes the tensor without copy (ArrayView is used internally).
let reshaped = graph.reshape(random, &[6]);
// Evaluating multiple tensors at once.
// Note that although `random` node is required two times in this computation graph,
// it's evaluated only once since `eval()` is smart enough to avoid duplicated computations.
let pair: Vec<Result<NdArray, _>> = graph.eval(&[mul, reshaped], &[]);
});
Implementations
Gets the i
th float value of this tensor.
Index i
can be negative.
use ndarray::{self, array};
use autograd::{self as ag, tensor::Variable};
ag::with(|g| {
let a = g.variable(array![[2., 3.], [4., 5.]]);
let b = a.access_elem(2);
assert_eq!(b.eval(&[]).unwrap()[ndarray::IxDyn(&[])], 4.);
});
Returns a mutable ref of the graph to which this tensor belongs.
Evaluates this tensor as an ndarray::Array<F, ndarray::IxDyn>
.
use ndarray::array;
use autograd as ag;
ag::with(|g| {
let a = g.zeros(&[2]);
assert_eq!(a.eval(&[]), Ok(array![0., 0.].into_dyn()));
});
See also Graph::eval.
Retruns a Feed
assigning a given value to this (placeholder) tensor.
Ensure that the return value is passed to ag::Eval
, ag::eval
or Tensor::eval
.
use ndarray::array;
use autograd as ag;
ag::with(|g| {
let x = g.placeholder(&[2]);
// Fills the placeholder with an ArrayView, then eval.
let value = array![1., 1.];
x.eval(&[
x.given(value.view())
]);
});
Creates a new TensorBuilder.
Sets a hook that displays the evaluation result of the receiver tensor to stderr.
use autograd as ag;
ag::with(|g| {
let a: ag::Tensor<f32> = g.zeros(&[4, 2]).show();
a.eval(&[]);
// [[0.0, 0.0],
// [0.0, 0.0],
// [0.0, 0.0],
// [0.0, 0.0]] shape=[4, 2], strides=[2, 1], layout=C (0x1)
});
Sets a hook that displays the evaluation result of the receiver tensor to stderr, with given prefix.
use autograd as ag;
ag::with(|g| {
let a: ag::Tensor<f32> = g.zeros(&[4, 2]).show_with("My value:");
a.eval(&[]);
// My value:
// [[0.0, 0.0],
// [0.0, 0.0],
// [0.0, 0.0],
// [0.0, 0.0]] shape=[4, 2], strides=[2, 1], layout=C (0x1)
});
Sets a hook that displays the shape of the evaluated receiver tensor to stderr.
use autograd as ag;
ag::with(|g| {
let a: ag::Tensor<f32> = g.zeros(&[2, 3]).show_shape();
a.eval(&[]);
// [2, 3]
});
Sets a hook that displays the shape of the evaluated receiver tensor to stderr, with given prefix.
use autograd as ag;
ag::with(|g| {
let a: ag::Tensor<f32> = g.zeros(&[2, 3]).show_shape_with("My shape:");
a.eval(&[]);
// My shape:
// [2, 3]
});
Sets a hook that displays the given string after evaluation of the receiver tensor.
use autograd as ag;
ag::with(|g| {
let a: ag::Tensor<f32> = g.zeros(&[2, 3]).print("This is `a`");
a.eval(&[]);
// This is `a`
});
Sets a hook that calls the given closure after evaluation of the receiver tensor.
use autograd as ag;
ag::with(|g| {
let a: ag::Tensor<f32> = g.zeros(&[2, 3]).raw_hook(|arr| println!("{:?}", arr));
a.eval(&[]);
});
Returns the number of inputs of this tensor.
Returns the number of inputs of this tensor.
Input nodes used when backprop.
Trait Implementations
Auto Trait Implementations
impl<'graph, F> !RefUnwindSafe for Tensor<'graph, F>
impl<'graph, F> !UnwindSafe for Tensor<'graph, F>
Blanket Implementations
Mutably borrows from an owned value. Read more