|
| 1 | +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 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 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +=======================================================================*/ |
| 15 | +package org.tensorflow.framework.constraints; |
| 16 | + |
| 17 | +import org.tensorflow.Operand; |
| 18 | +import org.tensorflow.op.Ops; |
| 19 | +import org.tensorflow.types.family.TNumber; |
| 20 | + |
| 21 | +import static org.tensorflow.framework.utils.CastHelper.cast; |
| 22 | + |
| 23 | +/** Constrains the weights to have the norm between a lower bound and an upper bound. */ |
| 24 | +public class MinMaxNorm extends Constraint { |
| 25 | + public static final double MIN_VALUE_DEFAULT = 0.0; |
| 26 | + public static final double MAX_VALUE_DEFAULT = 1.0; |
| 27 | + public static final double RATE_DEFAULT = 1.0; |
| 28 | + public static final int AXIS_DEFAULT = 0; |
| 29 | + |
| 30 | + /** the minimum norm for the incoming weights. */ |
| 31 | + private final double minValue; |
| 32 | + /** the maximum norm for the incoming weights. */ |
| 33 | + private final double maxValue; |
| 34 | + |
| 35 | + /** |
| 36 | + * rate for enforcing the constraint: weights will be rescaled to yield (1 - rate) * norm + rate * |
| 37 | + * norm.clip(min_value, max_value). Effectively, this means that rate=1.0 stands for strict |
| 38 | + * enforcement of the constraint, while rate<1.0 means that weights will be rescaled at each step |
| 39 | + * to slowly move towards a value inside the desired interval. |
| 40 | + */ |
| 41 | + private final double rate; |
| 42 | + |
| 43 | + /** axis along which to calculate weight norms. */ |
| 44 | + private final int[] axes; |
| 45 | + |
| 46 | + /** |
| 47 | + * Create a MinMaxNorm constraint using {@link #MIN_VALUE_DEFAULT} for the min value, {@link |
| 48 | + * #MAX_VALUE_DEFAULT} for the max value, {@link #RATE_DEFAULT} for the rate and {@link |
| 49 | + * #AXIS_DEFAULT} for the axis |
| 50 | + * |
| 51 | + * @param tf the TensorFlow Ops |
| 52 | + */ |
| 53 | + public MinMaxNorm(Ops tf) { |
| 54 | + this(tf, MIN_VALUE_DEFAULT, MAX_VALUE_DEFAULT, RATE_DEFAULT, AXIS_DEFAULT); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a MinMaxNorm constraint using {@link #RATE_DEFAULT} for the rate and {@link |
| 59 | + * #AXIS_DEFAULT} for the axis |
| 60 | + * |
| 61 | + * @param tf the TensorFlow Ops |
| 62 | + * @param minValue the minimum norm for the incoming weights. |
| 63 | + * @param maxValue the maximum norm for the incoming weights. |
| 64 | + */ |
| 65 | + public MinMaxNorm(Ops tf, double minValue, double maxValue) { |
| 66 | + this(tf, minValue, maxValue, RATE_DEFAULT, AXIS_DEFAULT); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Create a MinMaxNorm constraint |
| 71 | + * |
| 72 | + * @param tf the TensorFlow Ops |
| 73 | + * @param minValue the minimum norm for the incoming weights. |
| 74 | + * @param maxValue the maximum norm for the incoming weights. |
| 75 | + * @param rate the rate for enforcing the constraint. |
| 76 | + * @param axis integer, axis along which to calculate weight norms. |
| 77 | + */ |
| 78 | + public MinMaxNorm(Ops tf, double minValue, double maxValue, double rate, int axis) { |
| 79 | + this(tf, minValue, maxValue, rate, new int[] {axis}); |
| 80 | + } |
| 81 | + /** |
| 82 | + * Create a MinMaxNorm constraint |
| 83 | + * |
| 84 | + * @param tf the TensorFlow Ops |
| 85 | + * @param minValue the minimum norm for the incoming weights. |
| 86 | + * @param maxValue the maximum norm for the incoming weights. |
| 87 | + * @param rate the rate for enforcing the constraint. |
| 88 | + * @param axes integer, axis along which to calculate weight norms. |
| 89 | + */ |
| 90 | + public MinMaxNorm(Ops tf, double minValue, double maxValue, double rate, int[] axes) { |
| 91 | + super(tf); |
| 92 | + this.minValue = minValue; |
| 93 | + this.maxValue = maxValue; |
| 94 | + this.rate = rate; |
| 95 | + this.axes = axes; |
| 96 | + } |
| 97 | + |
| 98 | + /** {@inheritDoc} */ |
| 99 | + @Override |
| 100 | + public <T extends TNumber> Operand<T> call(Operand<T> weights) { |
| 101 | + Class<T> type = weights.type(); |
| 102 | + Ops tf = getTF(); |
| 103 | + Operand<T> norms = norm(weights, getAxes()); |
| 104 | + Operand<T> desired = |
| 105 | + tf.math.add( |
| 106 | + tf.math.mul( |
| 107 | + tf.dtypes.cast(tf.constant(this.getRate()), type), |
| 108 | + clip(norms, this.getMinValue(), this.getMaxValue())), |
| 109 | + tf.math.mul( |
| 110 | + tf.math.sub( |
| 111 | + tf.dtypes.cast(tf.constant(1), type), |
| 112 | + tf.dtypes.cast(tf.constant(this.getRate()), type)), |
| 113 | + norms)); |
| 114 | + |
| 115 | + return tf.math.mul( |
| 116 | + weights, tf.math.div(desired, tf.math.add(cast(tf, tf.constant(EPSILON), type), norms))); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Gets the minValue |
| 121 | + * |
| 122 | + * @return the minValue |
| 123 | + */ |
| 124 | + public double getMinValue() { |
| 125 | + return minValue; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Gets the maxValue |
| 130 | + * |
| 131 | + * @return the maxValue |
| 132 | + */ |
| 133 | + public double getMaxValue() { |
| 134 | + return maxValue; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Gets the rate |
| 139 | + * |
| 140 | + * @return the rate |
| 141 | + */ |
| 142 | + public double getRate() { |
| 143 | + return rate; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Gets the axes |
| 148 | + * |
| 149 | + * @return the axes |
| 150 | + */ |
| 151 | + public int[] getAxes() { |
| 152 | + return axes; |
| 153 | + } |
| 154 | +} |
0 commit comments