| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8dd1eec commit 3f0b1f9
17 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,7 +63,7 @@ Import TF.NET and Keras API in your project. | |||
| 63 | 63 | using static Tensorflow.Binding; | |
| 64 | 64 | using static Tensorflow.KerasApi; | |
| 65 | 65 | using Tensorflow; | |
| 66 | - using NumSharp; | ||
| 66 | + using Tensorflow.NumPy; | ||
| 67 | 67 | ``` | |
| 68 | 68 | ||
| 69 | 69 | Linear Regression in `Eager` mode: | |
@@ -162,10 +162,9 @@ Linear Regression in `Eager` mode: | |||
| 162 | 162 | #r "nuget: TensorFlow.Net" | |
| 163 | 163 | #r "nuget: TensorFlow.Keras" | |
| 164 | 164 | #r "nuget: SciSharp.TensorFlow.Redist" | |
| 165 | - #r "nuget: NumSharp" | ||
| 166 | 165 | ||
| 167 | - open NumSharp | ||
| 168 | 166 | open Tensorflow | |
| 167 | + open Tensorflow.NumPy | ||
| 169 | 168 | open type Tensorflow.Binding | |
| 170 | 169 | open type Tensorflow.KerasApi | |
| 171 | 170 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -203,13 +203,6 @@ public static IEnumerable<T> reversed<T>(IList<T> values) | |||
| 203 | 203 | yield return values[i]; | |
| 204 | 204 | } | |
| 205 | 205 | ||
| 206 | - public static T New<T>() where T : ITensorFlowObject, new() | ||
| 207 | - { | ||
| 208 | - var instance = new T(); | ||
| 209 | - instance.__init__(); | ||
| 210 | - return instance; | ||
| 211 | - } | ||
| 212 | - | ||
| 213 | 206 | [DebuggerStepThrough] | |
| 214 | 207 | public static void tf_with(ITensorFlowObject py, Action<ITensorFlowObject> action) | |
| 215 | 208 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -81,7 +81,7 @@ BackwardFunction GetGradientFunction(string op_name, | |||
| 81 | 81 | if (ops.gradientFunctions[op_name] == null) | |
| 82 | 82 | return new Tensor[op_inputs.Length]; | |
| 83 | 83 | ||
| 84 | - var gradients = ops.gradientFunctions[op_name](new EagerOperation | ||
| 84 | + var op = new EagerOperation | ||
| 85 | 85 | { | |
| 86 | 86 | Name = op_name, | |
| 87 | 87 | NumInputs = op_inputs.Length, | |
@@ -90,9 +90,9 @@ BackwardFunction GetGradientFunction(string op_name, | |||
| 90 | 90 | Outputs = op_outputs, | |
| 91 | 91 | SkipInputIndices = unneeded_gradients, | |
| 92 | 92 | Attrs = attrs | |
| 93 | - }, output_grads); | ||
| 93 | + }; | ||
| 94 | 94 | ||
| 95 | - return gradients; | ||
| 95 | + return ops.gradientFunctions[op_name](op, output_grads); | ||
| 96 | 96 | }; | |
| 97 | 97 | ||
| 98 | 98 | bool CouldForwardprop() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,15 +20,8 @@ namespace Tensorflow | |||
| 20 | 20 | { | |
| 21 | 21 | public interface ITensorFlowObject : IDisposable | |
| 22 | 22 | { | |
| 23 | - /// <summary> | ||
| 24 | - /// Called when the instance is created. | ||
| 25 | - /// </summary> | ||
| 26 | - void __init__(); | ||
| 27 | - | ||
| 28 | 23 | void __enter__(); | |
| 29 | 24 | ||
| 30 | 25 | void __exit__(); | |
| 31 | - | ||
| 32 | - void __del__(); | ||
| 33 | 26 | } | |
| 34 | 27 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,16 @@ | |||
| 1 | + using static Tensorflow.Binding; | ||
| 2 | + | ||
| 3 | + namespace Tensorflow.Keras.ArgsDefinition | ||
| 4 | + { | ||
| 5 | + public class LayerNormalizationArgs : LayerArgs | ||
| 6 | + { | ||
| 7 | + public Axis Axis { get; set; } = -1; | ||
| 8 | + public float Epsilon { get; set; } = 1e-3f; | ||
| 9 | + public bool Center { get; set; } = true; | ||
| 10 | + public bool Scale { get; set; } = true; | ||
| 11 | + public IInitializer BetaInitializer { get; set; } = tf.zeros_initializer; | ||
| 12 | + public IInitializer GammaInitializer { get; set; } = tf.ones_initializer; | ||
| 13 | + public IRegularizer BetaRegularizer { get; set; } | ||
| 14 | + public IRegularizer GammaRegularizer { get; set; } | ||
| 15 | + } | ||
| 16 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,8 +89,8 @@ tf.net 0.6x.x aligns with TensorFlow v2.6.x native library.</PackageReleaseNotes | |||
| 89 | 89 | </ItemGroup> | |
| 90 | 90 | ||
| 91 | 91 | <ItemGroup> | |
| 92 | - <PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.139" /> | ||
| 93 | - <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" /> | ||
| 92 | + <PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.144" /> | ||
| 93 | + <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> | ||
| 94 | 94 | <PackageReference Include="Protobuf.Text" Version="0.5.0" /> | |
| 95 | 95 | <PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" /> | |
| 96 | 96 | </ItemGroup> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -316,7 +316,7 @@ protected override Tensors Call(Tensors inputs, Tensor state = null, bool? train | |||
| 316 | 316 | var outputs = node.Layer.Apply(layer_inputs, is_training: training ?? false); | |
| 317 | 317 | foreach (var output in outputs.Where(x => x != null)) | |
| 318 | 318 | tf.Logger.Information($"Depth {depth}: {node.Layer}: {node.Layer.Name} {output.shape}"); | |
| 319 | - // Update tensor_dict for next input | ||
| 319 | + // Update tensor_dict for next or later input | ||
| 320 | 320 | foreach (var (x_id, y) in zip(node.Outputs.Select(x => x.Id), outputs)) | |
| 321 | 321 | tensor_dict[x_id] = new Queue<Tensor>(Enumerable.Range(0, tensor_usage_count[x_id]).Select(x => y)); | |
| 322 | 322 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -635,6 +635,21 @@ public Tensor max_pooling2d(Tensor inputs, | |||
| 635 | 635 | return layer.Apply(inputs); | |
| 636 | 636 | } | |
| 637 | 637 | ||
| 638 | + public Layer LayerNormalization(Axis? axis, | ||
| 639 | + float epsilon = 1e-3f, | ||
| 640 | + bool center = true, | ||
| 641 | + bool scale = true, | ||
| 642 | + IInitializer beta_initializer = null, | ||
| 643 | + IInitializer gamma_initializer = null) | ||
| 644 | + => new LayerNormalization(new LayerNormalizationArgs | ||
| 645 | + { | ||
| 646 | + Axis = axis ?? -1, | ||
| 647 | + Epsilon = epsilon, | ||
| 648 | + Center = center, | ||
| 649 | + Scale = scale, | ||
| 650 | + BetaInitializer = beta_initializer ?? tf.zeros_initializer | ||
| 651 | + }); | ||
| 652 | + | ||
| 638 | 653 | /// <summary> | |
| 639 | 654 | /// Leaky version of a Rectified Linear Unit. | |
| 640 | 655 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -218,7 +218,8 @@ private Tensor _fused_batch_norm(Tensor inputs, Tensor training) | |||
| 218 | 218 | beta, | |
| 219 | 219 | mean: moving_mean, | |
| 220 | 220 | variance: moving_variance, | |
| 221 | - epsilon: epsilon, is_training: true, | ||
| 221 | + epsilon: epsilon, | ||
| 222 | + is_training: true, | ||
| 222 | 223 | data_format: _data_format, | |
| 223 | 224 | exponential_avg_factor: exponential_avg_factor); | |
| 224 | 225 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,145 @@ | |||
| 1 | + /***************************************************************************** | ||
| 2 | + Copyright 2021 Haiping Chen. All Rights Reserved. | ||
| 3 | + | ||
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + you may not use this file except in compliance with the License. | ||
| 6 | + You may obtain a copy of the License at | ||
| 7 | + | ||
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + | ||
| 10 | + Unless required by applicable law or agreed to in writing, software | ||
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + See the License for the specific language governing permissions and | ||
| 14 | + limitations under the License. | ||
| 15 | + ******************************************************************************/ | ||
| 16 | + | ||
| 17 | + using System; | ||
| 18 | + using System.Collections.Generic; | ||
| 19 | + using System.Linq; | ||
| 20 | + using Tensorflow.Keras.ArgsDefinition; | ||
| 21 | + using Tensorflow.Keras.Engine; | ||
| 22 | + using Tensorflow.Keras.Utils; | ||
| 23 | + using static Tensorflow.Binding; | ||
| 24 | + | ||
| 25 | + namespace Tensorflow.Keras.Layers | ||
| 26 | + { | ||
| 27 | + public class LayerNormalization : Layer | ||
| 28 | + { | ||
| 29 | + LayerNormalizationArgs args; | ||
| 30 | + | ||
| 31 | + float epsilon => args.Epsilon; | ||
| 32 | + bool center => args.Center; | ||
| 33 | + bool scale => args.Scale; | ||
| 34 | + bool _fused; | ||
| 35 | + int[] axis; | ||
| 36 | + string _data_format; | ||
| 37 | + Shape kernel_size; | ||
| 38 | + IInitializer beta_initializer => args.BetaInitializer; | ||
| 39 | + IInitializer gamma_initializer => args.GammaInitializer; | ||
| 40 | + IRegularizer gamma_regularizer => args.GammaRegularizer; | ||
| 41 | + IVariableV1 gamma; | ||
| 42 | + IVariableV1 beta; | ||
| 43 | + IVariableV1 moving_mean; | ||
| 44 | + IVariableV1 moving_variance; | ||
| 45 | + | ||
| 46 | + public LayerNormalization(LayerNormalizationArgs args) : base(args) | ||
| 47 | + { | ||
| 48 | + this.args = args; | ||
| 49 | + axis = args.Axis.axis; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + protected override void build(Tensors inputs) | ||
| 53 | + { | ||
| 54 | + Shape input_shape = inputs.shape; | ||
| 55 | + var ndims = input_shape.ndim; | ||
| 56 | + foreach (var (idx, x) in enumerate(axis)) | ||
| 57 | + if (x < 0) | ||
| 58 | + axis[idx] = ndims + x; | ||
| 59 | + | ||
| 60 | + var axis_to_dim = new Dictionary<int, int>(); | ||
| 61 | + foreach (var x in axis) | ||
| 62 | + axis_to_dim[x] = (int)input_shape[x]; | ||
| 63 | + | ||
| 64 | + inputSpec = new InputSpec(ndim: ndims, axes: axis_to_dim); | ||
| 65 | + var param_dtype = DType == TF_DataType.DtInvalid ? TF_DataType.TF_FLOAT : DType; | ||
| 66 | + var param_shape = inputSpec.AllAxisDim; | ||
| 67 | + | ||
| 68 | + if (scale) | ||
| 69 | + gamma = add_weight("gamma", | ||
| 70 | + param_shape, | ||
| 71 | + dtype: param_dtype, | ||
| 72 | + initializer: gamma_initializer, | ||
| 73 | + trainable: true); | ||
| 74 | + | ||
| 75 | + if (center) | ||
| 76 | + beta = add_weight("beta", | ||
| 77 | + param_shape, | ||
| 78 | + dtype: param_dtype, | ||
| 79 | + initializer: beta_initializer, | ||
| 80 | + trainable: true); | ||
| 81 | + | ||
| 82 | + _fused = _fused_can_be_used(ndims); | ||
| 83 | + | ||
| 84 | + built = true; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + bool _fused_can_be_used(int ndims) | ||
| 88 | + { | ||
| 89 | + var can_use_fused = false; | ||
| 90 | + if (axis.Last() == ndims - 1 && axis.Last() - axis[0] == len(axis) - 1) | ||
| 91 | + can_use_fused = true; | ||
| 92 | + if (epsilon < 1.001e-5 || DType != tf.float32) | ||
| 93 | + can_use_fused = false; | ||
| 94 | + return can_use_fused; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + public override Shape ComputeOutputShape(Shape input_shape) | ||
| 98 | + { | ||
| 99 | + return input_shape; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) | ||
| 103 | + { | ||
| 104 | + Tensors outputs = null; | ||
| 105 | + var inputs_dtype = inputs.dtype.as_base_dtype(); | ||
| 106 | + var input_shape = inputs.shape; | ||
| 107 | + var ndims = len(input_shape); | ||
| 108 | + var broadcast_shape = range(ndims).Select(x => 1).ToArray(); | ||
| 109 | + foreach (var dim in axis) | ||
| 110 | + broadcast_shape[dim] = input_shape.as_int_list()[dim]; | ||
| 111 | + | ||
| 112 | + if (_fused) | ||
| 113 | + { | ||
| 114 | + var tensor_shape = tf.shape(inputs); | ||
| 115 | + var pre_dim = tf.constant(1); | ||
| 116 | + var in_dim = tf.constant(1); | ||
| 117 | + foreach (var dim in range(ndims)) | ||
| 118 | + { | ||
| 119 | + var dim_tensor = tensor_shape[dim]; | ||
| 120 | + if (dim < axis[0]) | ||
| 121 | + pre_dim = pre_dim * dim_tensor; | ||
| 122 | + else | ||
| 123 | + in_dim = in_dim * dim_tensor; | ||
| 124 | + } | ||
| 125 | + inputs = tf.reshape(inputs, new object[] { 1, pre_dim, in_dim, 1 }); | ||
| 126 | + | ||
| 127 | + var scale = tf.ones(new Shape((int)pre_dim), dtype: DType); | ||
| 128 | + var offset = tf.zeros(new Shape((int)pre_dim), dtype: DType); | ||
| 129 | + | ||
| 130 | + /*outputs = tf.nn.fused_batch_norm( | ||
| 131 | + inputs, | ||
| 132 | + scale: scale, | ||
| 133 | + offset: offset, | ||
| 134 | + epsilon: epsilon, | ||
| 135 | + data_format: "NCHW");*/ | ||
| 136 | + } | ||
| 137 | + else | ||
| 138 | + { | ||
| 139 | + | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + return outputs; | ||
| 143 | + } | ||
| 144 | + } | ||
| 145 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments