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 or Eval::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 the graph to which this tensor belongs.

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 id of this tensor in this graph.

Returns the number of inputs of this tensor.

Returns the number of inputs of this tensor.

Returns true if this node has no incoming nodes.

Input nodes used when backprop.

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the conversion.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.