You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Normalization.cs 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*****************************************************************************
  2. Copyright 2023 Haiping Chen. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using Tensorflow.Keras.ArgsDefinition;
  14. namespace Tensorflow.Keras.Layers
  15. {
  16. public class Normalization : PreprocessingLayer
  17. {
  18. NormalizationArgs _args;
  19. int[] axis;
  20. int[] _reduce_axis;
  21. IVariableV1 adapt_mean, adapt_variance, count;
  22. Tensor mean, variance;
  23. Shape _broadcast_shape;
  24. float? input_mean, input_variance;
  25. TF_DataType compute_dtype = tf.float32;
  26. public Normalization(NormalizationArgs args) : base(args)
  27. {
  28. _args = args;
  29. if (args.Axis == null)
  30. {
  31. axis = new int[0];
  32. }
  33. else
  34. {
  35. axis = args.Axis.axis;
  36. }
  37. input_mean = args.Mean;
  38. input_variance = args.Variance;
  39. }
  40. public override void build(Shape input_shape)
  41. {
  42. base.build(input_shape);
  43. var ndim = input_shape.ndim;
  44. foreach (var (idx, x) in enumerate(axis))
  45. if (x < 0)
  46. axis[idx] = ndim + x;
  47. var _keep_axis = axis.Select(d => d >= 0 ? d : d + ndim).ToArray();
  48. _reduce_axis = range(ndim).Where(d => !_keep_axis.Contains(d)).ToArray();
  49. var _reduce_axis_mask = range(ndim).Select(d => _keep_axis.Contains(d) ? 0 : 1).ToArray();
  50. // Broadcast any reduced axes.
  51. _broadcast_shape = new Shape(range(ndim).Select(d => _keep_axis.Contains(d) ? input_shape.dims[d] : 1).ToArray());
  52. var mean_and_var_shape = _keep_axis.Select(d => input_shape.dims[d]).ToArray();
  53. var param_dtype = DType == TF_DataType.DtInvalid ? TF_DataType.TF_FLOAT : DType;
  54. var param_shape = input_shape;
  55. if(input_mean == null)
  56. {
  57. adapt_mean = add_weight("mean",
  58. mean_and_var_shape,
  59. dtype: tf.float32,
  60. initializer: tf.zeros_initializer,
  61. trainable: false);
  62. adapt_variance = add_weight("variance",
  63. mean_and_var_shape,
  64. dtype: tf.float32,
  65. initializer: tf.ones_initializer,
  66. trainable: false);
  67. count = add_weight("count",
  68. Shape.Scalar,
  69. dtype: tf.int64,
  70. initializer: tf.zeros_initializer,
  71. trainable: false);
  72. finalize_state();
  73. }
  74. else
  75. {
  76. mean = input_mean * np.ones(mean_and_var_shape);
  77. variance = input_variance * np.ones(mean_and_var_shape);
  78. mean = tf.reshape(mean, _broadcast_shape);
  79. variance = tf.reshape(variance, _broadcast_shape);
  80. mean = tf.cast(mean, compute_dtype);
  81. variance = tf.cast(variance, compute_dtype);
  82. }
  83. }
  84. public override void reset_state()
  85. {
  86. if (input_mean != null && !built)
  87. {
  88. return;
  89. }
  90. adapt_mean.assign(tf.zeros_like(adapt_mean.AsTensor()));
  91. adapt_variance.assign(tf.ones_like(adapt_variance.AsTensor()));
  92. count.assign(tf.zeros_like(count.AsTensor()));
  93. }
  94. public override void finalize_state()
  95. {
  96. if (input_mean != null && !built)
  97. {
  98. return;
  99. }
  100. mean = tf.reshape(adapt_mean.AsTensor(), _broadcast_shape);
  101. variance = tf.reshape(adapt_variance.AsTensor(), _broadcast_shape);
  102. }
  103. public override void update_state(Tensor data)
  104. {
  105. data = tf.cast(data, adapt_mean.dtype);
  106. var (batch_mean, batch_variance) = tf.nn.moments(data, axes: _reduce_axis);
  107. var batch_shape = tf.shape(data, out_type: count.dtype);
  108. var batch_count = constant_op.constant(1L);
  109. if (_reduce_axis != null)
  110. {
  111. var batch_reduce_shape = tf.gather(batch_shape, constant_op.constant(_reduce_axis));
  112. batch_count = tf.reduce_prod(batch_reduce_shape);
  113. }
  114. var total_count = batch_count + count.AsTensor();
  115. var batch_weight = tf.cast(batch_count, dtype: compute_dtype) / tf.cast(
  116. total_count, dtype: compute_dtype);
  117. var existing_weight = 1.0 - batch_weight;
  118. var total_mean = adapt_mean.AsTensor() * existing_weight + batch_mean * batch_weight;
  119. var total_variance = (
  120. adapt_variance.AsTensor() + tf.square(adapt_mean.AsTensor() - total_mean)
  121. ) * existing_weight + (
  122. batch_variance + tf.square(batch_mean - total_mean)
  123. ) * batch_weight;
  124. adapt_mean.assign(total_mean);
  125. adapt_variance.assign(total_variance);
  126. count.assign(total_count);
  127. }
  128. public override Shape ComputeOutputShape(Shape input_shape)
  129. {
  130. return input_shape;
  131. }
  132. public override void adapt(Tensor data, int? batch_size = null, int? steps = null)
  133. {
  134. base.adapt(data, batch_size: batch_size, steps: steps);
  135. }
  136. protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null)
  137. {
  138. if (_args.Invert)
  139. {
  140. return mean + (
  141. inputs * tf.maximum(tf.sqrt(variance), keras.backend.epsilon())
  142. );
  143. }
  144. else
  145. {
  146. return (inputs - mean) / tf.maximum(
  147. tf.sqrt(variance), keras.backend.epsilon());
  148. }
  149. }
  150. }
  151. }