44using System . Text ;
55using Tensorflow . Keras . Engine ;
66using Tensorflow . Keras . Utils ;
7+ using static Tensorflow . Python ;
78
89namespace Tensorflow . Keras . Layers
910{
@@ -33,7 +34,8 @@ public class Layer : CheckpointableBase
3334 protected InputSpec input_spec ;
3435 protected bool supports_masking ;
3536 protected List < RefVariable > _trainable_weights ;
36- public string _name ;
37+ private string _name ;
38+ public string name => _name ;
3739 protected string _base_name ;
3840 protected bool _compute_previous_mask ;
3941 protected List < Operation > _updates ;
@@ -85,17 +87,24 @@ public Tensor __call__(Tensor[] inputs,
8587 // Handle Keras mask propagation from previous layer to current layer.
8688 Python . with ( ops . name_scope ( _name_scope ( ) ) , delegate
8789 {
88- if ( ! built )
90+ /* if (!built)
8991 {
9092 _maybe_build(inputs);
9193 built = true;
92- }
94+ }*/
9395
9496 if ( build_graph )
9597 {
9698 // Symbolic execution on symbolic tensors. We will attempt to build
9799 // the corresponding TF subgraph inside `backend.get_graph()`
98- var graph = backend . get_graph ( ) ;
100+ var graph = backend . get_graph ( ) . as_default ( ) ;
101+ with ( ops . name_scope ( _name_scope ( ) ) , delegate
102+ {
103+ // Build layer if applicable (if the `build` method has been
104+ // overridden).
105+ _maybe_build ( inputs [ 0 ] ) ;
106+ } ) ;
107+
99108 outputs = call ( inputs [ 0 ] , training : training ) ;
100109 _handle_activity_regularization ( inputs [ 0 ] , outputs ) ;
101110 _set_mask_metadata ( inputs [ 0 ] , outputs , null ) ;
@@ -130,13 +139,17 @@ protected virtual Tensor call(Tensor inputs, Tensor training = null)
130139
131140 protected virtual string _name_scope ( )
132141 {
133- return null ;
142+ return name ;
134143 }
135144
136- protected void _maybe_build ( Tensor [ ] inputs )
145+ protected void _maybe_build ( Tensor input )
137146 {
138- var input_list = inputs ;
139- build ( input_list [ 0 ] . GetShape ( ) ) ;
147+ // Check input assumptions set before layer building, e.g. input rank.
148+ if ( built )
149+ return ;
150+
151+ build ( input . GetShape ( ) ) ;
152+ built = true ;
140153 }
141154
142155 protected virtual void build ( TensorShape input_shape )
@@ -160,7 +173,7 @@ protected virtual RefVariable add_weight(string name,
160173 var variable = _add_variable_with_custom_getter ( name ,
161174 shape ,
162175 dtype : dtype ,
163- getter : getter == null ? base_layer_utils . make_variable : getter ,
176+ // getter: getter == null ? base_layer_utils.make_variable : getter,
164177 overwrite : true ,
165178 initializer : initializer ,
166179 trainable : trainable . Value ) ;
@@ -176,12 +189,12 @@ protected virtual void add_update(Tensor[] updates, bool inputs = false)
176189 _updates . AddRange ( updates_op ) ;
177190 }
178191
179- protected virtual void _init_set_name ( string name )
192+ protected virtual void _init_set_name ( string name , bool zero_based = true )
180193 {
181- string base_name = name ;
182194 if ( name == null )
183- ( _name , base_name ) = _make_unique_name ( ) ;
184- _base_name = base_name ;
195+ _name = base_layer_utils . unique_layer_name ( generic_utils . to_snake_case ( this . GetType ( ) . Name ) , zero_based : zero_based ) ;
196+ else
197+ _name = name ;
185198 }
186199
187200 protected virtual ( string , string ) _make_unique_name ( )
0 commit comments