pub struct Scope { /* private fields */ }
Expand description

A Scope object represents a set of related TensorFlow ops that have the same properties such as a common name prefix.

A Scope object is a container for TensorFlow Op properties. Op constructors get a Scope object as a mandatory first argument and the constructed op acquires the properties in the object.

A simple example:

let mut root = Scope::new_root_scope();
let c1 = ops::constant(Tensor::new(&[1, 2]).with_values(&[1, 1])?, &mut root)?;
let c2 = ops::constant(Tensor::new(&[2, 1]).with_values(&[41, 1])?, &mut root)?;
let m = ops::mat_mul(c1, c2, &mut root)?;

Scope hierarchy

The Scope class provides various with_* functions that create a new scope. The new scope typically has one property changed while other properties are inherited from the parent scope. new_sub_scope(name) method appends name to the prefix of names for ops created within the scope, and with_op_name() changes the suffix which otherwise defaults to the type of the op.

Name examples:

let mut root = Scope::new_root_scope();
let mut linear = root.new_sub_scope("linear");
let w = Variable::builder()
  .const_initial_value(
    Tensor::new(&[2, 2])
      .with_values(&[0.0f32, 0.0, 0.0, 0.0])?)
  .build(&mut linear.with_op_name("W"))?;
assert_eq!(w.name(), "linear/W");
let b = Variable::builder()
  .const_initial_value(
    Tensor::new(&[2])
      .with_values(&[0.0f32, 0.0])?)
  .build(&mut linear.with_op_name("b"))?;
assert_eq!(b.name(), "linear/b");
let x = ops::constant(
  Tensor::new(&[2, 2])
    .with_values(&[1.0f32, 2.0, 3.0, 4.0])?,
   &mut linear)?;
assert_eq!(x.name()?, "linear/Const");
let m = ops::mat_mul(x, w.output().clone(), &mut linear)?;
assert_eq!(m.name()?, "linear/MatMul");
let r = ops::bias_add(m, b.output().clone(), &mut linear)?;
assert_eq!(r.name()?, "linear/BiasAdd");

Scope lifetime

A new scope is created by calling Scope::new_root_scope. This creates some resources that are shared by all the child scopes that inherit from this scope, directly or transitively. For instance, a new scope creates a new Graph object to which operations are added when the new scope or its children are used by an Op constructor.

Implementations

Return a new scope. This creates a new graph and all operations constructed in this graph should use the returned object as the “root” scope.

Return a new scope. Ops created with this scope will have name/child_scope_name as the prefix. The actual name will be unique in the current scope. All other properties are inherited from the current scope. If child_scope_name is empty, the / is elided.

Return a new scope. All ops created within the returned scope will have names of the form scope_name/name[_suffix]

Return a unique name, using default_name if an op name has not been specified.

Return a new scope. All ops created within the returned scope will have their device field set to device.

Return a new scope. All ops created within the returned scope will have as control dependencies the union of operations in control_deps and the control dependencies of the current scope.

Return a new scope. All ops created within the returned scope will have no control dependencies on other operations.

Return a new scope. All ops created with the new scope will have kernel_label as the value for their ‘_kernel’ attribute.

Returns a new scope. All ops created within the returned scope will have their ‘_XlaCluster’ attribute set to xla_cluster.

Returns the graph being built by the scope.

Returns the graph being built by the scope.

Trait Implementations

Formats the value using the given formatter. 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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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.