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
impl Scope
sourcepub fn new_root_scope() -> Scope
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.
sourcepub fn new_sub_scope(&self, name: &str) -> Scope
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.
sourcepub fn with_op_name(&self, name: &str) -> Scope
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]
sourcepub fn get_unique_name_for_op(&self, default_name: &str) -> String
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.
sourcepub fn with_device(&self, device: &str) -> Scope
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
.
sourcepub fn with_control_dependencies(&self, control_deps: &[Operation]) -> Scope
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.
sourcepub fn with_no_control_dependencies(&self) -> Scope
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.
sourcepub fn with_kernel_label(&self, kernel_label: &str) -> Scope
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.
sourcepub fn with_xla_cluster(&self, xla_cluster: &str) -> Scope
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.