|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Tensorflow.Keras.Engine; |
| 5 | + |
| 6 | +namespace Tensorflow.Layers |
| 7 | +{ |
| 8 | + public class Layer : Keras.Engine.Layer |
| 9 | + { |
| 10 | + protected bool trainable; |
| 11 | + protected string _name; |
| 12 | + protected TF_DataType _dtype; |
| 13 | + protected Graph _graph; |
| 14 | + protected string _base_name; |
| 15 | + protected VariableScope _scope; |
| 16 | + protected VariableScope _current_scope; |
| 17 | + /// <summary> |
| 18 | + /// A stateful layer is a layer whose updates are run during inference too, |
| 19 | + /// for instance stateful RNNs. |
| 20 | + /// </summary> |
| 21 | + protected bool stateful; |
| 22 | + /// <summary> |
| 23 | + /// Provides information about which inputs are compatible with the layer. |
| 24 | + /// </summary> |
| 25 | + protected InputSpec input_spec; |
| 26 | + protected bool supports_masking; |
| 27 | + protected bool? _reuse; |
| 28 | + |
| 29 | + public Layer(bool trainable = true, |
| 30 | + string name = null, |
| 31 | + TF_DataType dtype = TF_DataType.DtInvalid, |
| 32 | + bool? _reuse = null) |
| 33 | + { |
| 34 | + this.trainable = trainable; |
| 35 | + this.stateful = false; |
| 36 | + this._reuse = _reuse; |
| 37 | + this.built = false; |
| 38 | + this.supports_masking = false; |
| 39 | + _init_set_name(name); |
| 40 | + } |
| 41 | + |
| 42 | + public Tensor apply(Tensor inputs) |
| 43 | + { |
| 44 | + return __call__(inputs); |
| 45 | + } |
| 46 | + |
| 47 | + public Tensor __call__(Tensor inputs, |
| 48 | + VariableScope scope = null) |
| 49 | + { |
| 50 | + _set_scope(scope); |
| 51 | + _graph = ops._get_graph_from_inputs(new List<Tensor> { inputs }, graph: _graph); |
| 52 | + |
| 53 | + variable_scope scope_context_manager = null; |
| 54 | + if (built) |
| 55 | + { |
| 56 | + |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + scope_context_manager = tf.variable_scope(_scope, |
| 61 | + auxiliary_name_scope: false); |
| 62 | + } |
| 63 | + |
| 64 | + Python.with(scope_context_manager, scope2 => _current_scope = scope2); |
| 65 | + // Actually call layer |
| 66 | + var outputs = base.__call__(inputs); |
| 67 | + |
| 68 | + throw new NotImplementedException(""); |
| 69 | + } |
| 70 | + |
| 71 | + protected virtual void add_weight() |
| 72 | + { |
| 73 | + var default_graph = ops.get_default_graph(); |
| 74 | + Graph init_graph = null; |
| 75 | + RefVariable[] existing_variables = null; |
| 76 | + |
| 77 | + if (default_graph.building_function) |
| 78 | + { |
| 79 | + throw new NotImplementedException("add_weight"); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + init_graph = default_graph; |
| 84 | + existing_variables = variables.global_variables().ToArray(); |
| 85 | + } |
| 86 | + |
| 87 | + var dtype = TF_DataType.TF_FLOAT; |
| 88 | + _set_scope(); |
| 89 | + var reuse = built || (_reuse != null && _reuse.Value); |
| 90 | + Python.with(tf.variable_scope(_scope, |
| 91 | + reuse: reuse, |
| 92 | + auxiliary_name_scope: false), scope => |
| 93 | + { |
| 94 | + _current_scope = scope; |
| 95 | + Python.with(new ops.name_scope(_name_scope()), delegate |
| 96 | + { |
| 97 | + |
| 98 | + |
| 99 | + }); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private void _init_set_name(string name) |
| 104 | + { |
| 105 | + if (string.IsNullOrEmpty(name)) |
| 106 | + (_name, _base_name) = _make_unique_name(); |
| 107 | + } |
| 108 | + |
| 109 | + private (string, string) _make_unique_name() |
| 110 | + { |
| 111 | + string base_name = "conv2d"; |
| 112 | + string name = base_layer_utils.unique_layer_name(base_name); |
| 113 | + return (name, base_name); |
| 114 | + } |
| 115 | + |
| 116 | + protected override string _name_scope() |
| 117 | + { |
| 118 | + return _current_scope.original_name_scope; |
| 119 | + } |
| 120 | + |
| 121 | + private void _set_scope(VariableScope scope = null) |
| 122 | + { |
| 123 | + if (_scope == null) |
| 124 | + { |
| 125 | + Python.with(tf.variable_scope(scope, default_name: _base_name), captured_scope => |
| 126 | + { |
| 127 | + _scope = captured_scope; |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments