Struct tensorflow::Scope

source ·
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§

source§

impl Scope

source

pub fn new_root_scope() -> Scope

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.

source

pub fn new_sub_scope(&self, name: &str) -> 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.

source

pub fn with_op_name(&self, name: &str) -> Scope

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

source

pub fn get_unique_name_for_op(&self, default_name: &str) -> String

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

source

pub fn with_device(&self, device: &str) -> Scope

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

source

pub fn with_control_dependencies(&self, control_deps: &[Operation]) -> Scope

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.

source

pub fn with_no_control_dependencies(&self) -> Scope

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

source

pub fn with_kernel_label(&self, kernel_label: &str) -> Scope

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

source

pub fn with_xla_cluster(&self, xla_cluster: &str) -> Scope

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

source

pub fn graph(&self) -> impl Deref<Target = Graph> + '_

Returns the graph being built by the scope.

source

pub fn graph_mut(&mut self) -> impl DerefMut<Target = Graph> + '_

Returns the graph being built by the scope.

Trait Implementations§

source§

impl Debug for Scope

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Scope

§

impl !Send for Scope

§

impl !Sync for Scope

§

impl Unpin for Scope

§

impl !UnwindSafe for Scope

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.