[][src]Struct autograd::tensor::Tensor

pub struct Tensor<'graph, F: Float> { /* fields omitted */ }

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

impl<'graph, F: Float> Tensor<'graph, F>[src]

pub fn access_elem(self, i: isize) -> Tensor<'graph, F>[src]

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.);
});

impl<'graph, F: Float> Tensor<'graph, F>[src]

pub fn graph(&self) -> &'graph Graph<F>[src]

Returns the graph to which this tensor belongs.

pub fn eval<'v>(
    &self,
    feeds: &'v [Feed<'v, F>]
) -> Result<NdArray<F>, EvalError>
[src]

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.

pub fn given<D>(self, value: ArrayView<F, D>) -> Feed<F> where
    D: Dimension
[src]

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())
    ]);
});

pub fn builder() -> TensorBuilder<F>[src]

Creates a new TensorBuilder.

pub fn show(self) -> Tensor<'graph, F>[src]

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)
    });

pub fn show_with(self, what: &'static str) -> Tensor<'graph, F>[src]

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)
});

pub fn show_shape(self) -> Tensor<'graph, F>[src]

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]
});

pub fn show_shape_with(self, what: &'static str) -> Tensor<'graph, F>[src]

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]
});

pub fn print(self, what: &'static str) -> Tensor<'graph, F>[src]

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`
});

pub fn raw_hook<FUN: Fn(&NdArrayView<F>) + 'static + Send + Sync>(
    self,
    f: FUN
) -> Tensor<'graph, F>
[src]

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(&[]);
});

pub fn id(&self) -> usize[src]

Returns the id of this tensor in this graph.

pub fn num_inputs(&self) -> usize[src]

Returns the number of inputs of this tensor.

pub fn is_source(&self) -> bool[src]

Returns true if this node has no incoming nodes.

pub fn get_backprop_inputs(&self) -> &[Input][src]

Input nodes used when backprop.

This is same as inputs in most cases.

pub fn is_placeholder(&self) -> bool[src]

pub fn clone_persistent_array(&self) -> Option<NdArray<F>>[src]

pub fn get_variable_array(&self) -> Option<&Arc<RwLock<NdArray<F>>>>[src]

pub fn get_variable_array_ptr(&self) -> Option<*const RwLock<NdArray<F>>>[src]

pub fn lock_variable_array(&self) -> Option<RwLockReadGuard<NdArray<F>>>[src]

pub fn lock_variable_array_mut(&self) -> Option<RwLockWriteGuard<NdArray<F>>>[src]

pub fn is_differentiable(&self) -> bool[src]

Trait Implementations

impl<'r, 'b, T: Float> Add<&'r Tensor<'b, T>> for f64[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'r, 'b, T: Float> Add<&'r Tensor<'b, T>> for f32[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'r, 'b, T: Float> Add<&'r Tensor<'b, T>> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'l, 'r, 'b, T: Float> Add<&'r Tensor<'b, T>> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'b, T: Float> Add<T> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'l, 'b, T: Float> Add<T> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'r, 'b, T: Float> Add<Tensor<'b, T>> for f64[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'r, 'b, T: Float> Add<Tensor<'b, T>> for f32[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'b, T: Float> Add<Tensor<'b, T>> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'l, 'b, T: Float> Add<Tensor<'b, T>> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the + operator.

impl<'b, T: Float> AsRef<Tensor<'b, T>> for Tensor<'b, T>[src]

impl<'graph, T: Float> AsTensor<'graph, T> for Tensor<'graph, T>[src]

impl<'graph, F: Clone + Float> Clone for Tensor<'graph, F>[src]

impl<'graph, F: Copy + Float> Copy for Tensor<'graph, F>[src]

impl<'r, 'b, T: Float> Div<&'r Tensor<'b, T>> for f64[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'r, 'b, T: Float> Div<&'r Tensor<'b, T>> for f32[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'r, 'b, T: Float> Div<&'r Tensor<'b, T>> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'l, 'r, 'b, T: Float> Div<&'r Tensor<'b, T>> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'b, T: Float> Div<T> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'l, 'b, T: Float> Div<T> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'r, 'b, T: Float> Div<Tensor<'b, T>> for f64[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'r, 'b, T: Float> Div<Tensor<'b, T>> for f32[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'b, T: Float> Div<Tensor<'b, T>> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'l, 'b, T: Float> Div<Tensor<'b, T>> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the / operator.

impl<'r, 'b, T: Float> Mul<&'r Tensor<'b, T>> for f64[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'r, 'b, T: Float> Mul<&'r Tensor<'b, T>> for f32[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'r, 'b, T: Float> Mul<&'r Tensor<'b, T>> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'l, 'r, 'b, T: Float> Mul<&'r Tensor<'b, T>> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'b, T: Float> Mul<T> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'l, 'b, T: Float> Mul<T> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'r, 'b, T: Float> Mul<Tensor<'b, T>> for f64[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'r, 'b, T: Float> Mul<Tensor<'b, T>> for f32[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'b, T: Float> Mul<Tensor<'b, T>> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'l, 'b, T: Float> Mul<Tensor<'b, T>> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the * operator.

impl<'r, 'b, T: Float> Sub<&'r Tensor<'b, T>> for f64[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'r, 'b, T: Float> Sub<&'r Tensor<'b, T>> for f32[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'r, 'b, T: Float> Sub<&'r Tensor<'b, T>> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'l, 'r, 'b, T: Float> Sub<&'r Tensor<'b, T>> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'b, T: Float> Sub<T> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'l, 'b, T: Float> Sub<T> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'r, 'b, T: Float> Sub<Tensor<'b, T>> for f64[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'r, 'b, T: Float> Sub<Tensor<'b, T>> for f32[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'b, T: Float> Sub<Tensor<'b, T>> for Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

impl<'l, 'b, T: Float> Sub<Tensor<'b, T>> for &'l Tensor<'b, T>[src]

type Output = Tensor<'b, T>

The resulting type after applying the - operator.

Auto Trait Implementations

impl<'graph, F> !RefUnwindSafe for Tensor<'graph, F>

impl<'graph, F> !Send for Tensor<'graph, F>

impl<'graph, F> !Sync for Tensor<'graph, F>

impl<'graph, F> Unpin for Tensor<'graph, F>

impl<'graph, F> !UnwindSafe for Tensor<'graph, F>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,