| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -144,6 +144,20 @@ public Tensor max_pooling2d(Tensor inputs, | |||
| 144 | 144 | return layer.apply(inputs); | |
| 145 | 145 | } | |
| 146 | 146 | ||
| 147 | + /// <summary> | ||
| 148 | + /// Densely-connected layer class. aka fully-connected<br></br> | ||
| 149 | + /// `outputs = activation(inputs * kernel + bias)` | ||
| 150 | + /// </summary> | ||
| 151 | + /// <param name="inputs"></param> | ||
| 152 | + /// <param name="units">Python integer, dimensionality of the output space.</param> | ||
| 153 | + /// <param name="activation"></param> | ||
| 154 | + /// <param name="use_bias">Boolean, whether the layer uses a bias.</param> | ||
| 155 | + /// <param name="kernel_initializer"></param> | ||
| 156 | + /// <param name="bias_initializer"></param> | ||
| 157 | + /// <param name="trainable"></param> | ||
| 158 | + /// <param name="name"></param> | ||
| 159 | + /// <param name="reuse"></param> | ||
| 160 | + /// <returns></returns> | ||
| 147 | 161 | public Tensor dense(Tensor inputs, | |
| 148 | 162 | int units, | |
| 149 | 163 | IActivation activation = null, | |
@@ -160,7 +174,8 @@ public Tensor dense(Tensor inputs, | |||
| 160 | 174 | var layer = new Dense(units, activation, | |
| 161 | 175 | use_bias: use_bias, | |
| 162 | 176 | bias_initializer: bias_initializer, | |
| 163 | - kernel_initializer: kernel_initializer); | ||
| 177 | + kernel_initializer: kernel_initializer, | ||
| 178 | + trainable: trainable); | ||
| 164 | 179 | ||
| 165 | 180 | return layer.apply(inputs); | |
| 166 | 181 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,8 @@ You may obtain a copy of the License at | |||
| 14 | 14 | limitations under the License. | |
| 15 | 15 | ******************************************************************************/ | |
| 16 | 16 | ||
| 17 | + using Tensorflow.Operations; | ||
| 18 | + | ||
| 17 | 19 | namespace Tensorflow | |
| 18 | 20 | { | |
| 19 | 21 | public partial class tensorflow | |
@@ -211,6 +213,36 @@ public Tensor logical_xor(Tensor x, Tensor y, string name = "LogicalXor") | |||
| 211 | 213 | /// <returns></returns> | |
| 212 | 214 | public Tensor _clip_by_value(Tensor t, Tensor clip_value_min, Tensor clip_value_max, string name = null) | |
| 213 | 215 | => gen_math_ops._clip_by_value(t, clip_value_min, clip_value_max); | |
| 216 | + | ||
| 217 | + /// <summary> | ||
| 218 | + /// Clips tensor values to a specified min and max. | ||
| 219 | + /// </summary> | ||
| 220 | + /// <param name="t"> | ||
| 221 | + /// A <c>Tensor</c>. | ||
| 222 | + /// </param> | ||
| 223 | + /// <param name="clip_value_min"> | ||
| 224 | + /// A 0-D (scalar) <c>Tensor</c>, or a <c>Tensor</c> with the same shape | ||
| 225 | + /// as <c>t</c>. The minimum value to clip by. | ||
| 226 | + /// </param> | ||
| 227 | + /// <param name="clip_value_max"> | ||
| 228 | + /// A 0-D (scalar) <c>Tensor</c>, or a <c>Tensor</c> with the same shape | ||
| 229 | + /// as <c>t</c>. The maximum value to clip by. | ||
| 230 | + /// </param> | ||
| 231 | + /// <param name="name"> | ||
| 232 | + /// If specified, the created operation in the graph will be this one, otherwise it will be named 'ClipByValue'. | ||
| 233 | + /// </param> | ||
| 234 | + /// <returns> | ||
| 235 | + /// A clipped <c>Tensor</c> with the same shape as input 't'. | ||
| 236 | + /// The Operation can be fetched from the resulting Tensor, by fetching the Operation property from the result. | ||
| 237 | + /// </returns> | ||
| 238 | + /// <remarks> | ||
| 239 | + /// Given a tensor <c>t</c>, this operation returns a tensor of the same type and | ||
| 240 | + /// shape as <c>t</c> with its values clipped to <c>clip_value_min</c> and <c>clip_value_max</c>. | ||
| 241 | + /// Any values less than <c>clip_value_min</c> are set to <c>clip_value_min</c>. Any values | ||
| 242 | + /// greater than <c>clip_value_max</c> are set to <c>clip_value_max</c>. | ||
| 243 | + /// </remarks> | ||
| 244 | + public Tensor clip_by_value (Tensor t, Tensor clip_value_min, Tensor clip_value_max, string name = "ClipByValue") | ||
| 245 | + => gen_ops.clip_by_value(t, clip_value_min, clip_value_max, name); | ||
| 214 | 246 | ||
| 215 | 247 | public Tensor sub(Tensor a, Tensor b) | |
| 216 | 248 | => gen_math_ops.sub(a, b); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,8 +18,8 @@ namespace Tensorflow | |||
| 18 | 18 | { | |
| 19 | 19 | public partial class tensorflow | |
| 20 | 20 | { | |
| 21 | - public Tensor convert_to_tensor(object value, | ||
| 22 | - string name = null) => ops.convert_to_tensor(value, name: name); | ||
| 21 | + public Tensor convert_to_tensor(object value, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, TF_DataType preferred_dtype = TF_DataType.DtInvalid) | ||
| 22 | + => ops.convert_to_tensor(value, dtype, name, preferred_dtype); | ||
| 23 | 23 | ||
| 24 | 24 | public Tensor strided_slice(Tensor input, Tensor begin, Tensor end, Tensor strides = null, | |
| 25 | 25 | int begin_mask = 0, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,20 +14,192 @@ You may obtain a copy of the License at | |||
| 14 | 14 | limitations under the License. | |
| 15 | 15 | ******************************************************************************/ | |
| 16 | 16 | ||
| 17 | + using System; | ||
| 18 | + using static Tensorflow.Binding; | ||
| 19 | + | ||
| 17 | 20 | namespace Tensorflow.Operations.Activation | |
| 18 | 21 | { | |
| 22 | + public class sigmoid : IActivation | ||
| 23 | + { | ||
| 24 | + public Tensor Activate(Tensor x, string name = null) | ||
| 25 | + { | ||
| 26 | + return tf.sigmoid(x); | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public class tanh : IActivation | ||
| 31 | + { | ||
| 32 | + public Tensor Activate(Tensor x, string name = null) | ||
| 33 | + { | ||
| 34 | + return tf.tanh(x); | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public class leakyrelu : IActivation | ||
| 39 | + { | ||
| 40 | + private readonly float _alpha; | ||
| 41 | + | ||
| 42 | + public leakyrelu(float alpha = 0.3f) { | ||
| 43 | + _alpha = alpha; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + public Tensor Activate(Tensor x, string name = null) | ||
| 47 | + { | ||
| 48 | + return nn_ops.leaky_relu(x, _alpha); | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public class elu : IActivation | ||
| 53 | + { | ||
| 54 | + private readonly float _alpha; | ||
| 55 | + | ||
| 56 | + public elu(float alpha = 0.1f) | ||
| 57 | + { | ||
| 58 | + _alpha = alpha; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + public Tensor Activate(Tensor x, string name = null) | ||
| 62 | + { | ||
| 63 | + var res = gen_ops.elu(x); | ||
| 64 | + if (Math.Abs(_alpha - 0.1f) < 0.00001f) | ||
| 65 | + { | ||
| 66 | + return res; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + return array_ops.@where(x > 0, res, _alpha * res); | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + public class softmax : IActivation | ||
| 74 | + { | ||
| 75 | + private readonly int _axis; | ||
| 76 | + | ||
| 77 | + /// <summary>Initializes a new instance of the <see cref="T:System.Object"></see> class.</summary> | ||
| 78 | + public softmax(int axis = -1) | ||
| 79 | + { | ||
| 80 | + _axis = axis; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + public Tensor Activate(Tensor x, string name = null) | ||
| 84 | + { | ||
| 85 | + return nn_ops.softmax(x, _axis); | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + public class softplus : IActivation | ||
| 90 | + { | ||
| 91 | + public Tensor Activate(Tensor x, string name = null) | ||
| 92 | + { | ||
| 93 | + return gen_ops.softplus(x); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + public class softsign : IActivation | ||
| 98 | + { | ||
| 99 | + public Tensor Activate(Tensor x, string name = null) | ||
| 100 | + { | ||
| 101 | + return gen_ops.softsign(x); | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + public class linear : IActivation | ||
| 106 | + { | ||
| 107 | + public Tensor Activate(Tensor x, string name = null) | ||
| 108 | + { | ||
| 109 | + return x; | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + | ||
| 114 | + public class exponential : IActivation | ||
| 115 | + { | ||
| 116 | + public Tensor Activate(Tensor x, string name = null) | ||
| 117 | + { | ||
| 118 | + return tf.exp(x, name: name); | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + | ||
| 19 | 123 | public class relu : IActivation | |
| 20 | 124 | { | |
| 21 | - public Tensor Activate(Tensor features, string name = null) | ||
| 125 | + private readonly float _threshold; | ||
| 126 | + private readonly float _alpha; | ||
| 127 | + private readonly float? _maxValue; | ||
| 128 | + | ||
| 129 | + public relu(float threshold = 0f, float alpha = 0.2f, float? max_value = null) | ||
| 130 | + { | ||
| 131 | + _threshold = threshold; | ||
| 132 | + _alpha = alpha; | ||
| 133 | + _maxValue = max_value; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + public Tensor Activate(Tensor x, string name = null) | ||
| 22 | 137 | { | |
| 23 | - OpDefLibrary _op_def_lib = new OpDefLibrary(); | ||
| 138 | + //based on keras/backend.py | ||
| 139 | + if (Math.Abs(_alpha) > 0.000001f) | ||
| 140 | + { | ||
| 141 | + if (!_maxValue.HasValue && Math.Abs(_threshold) < 0.0001) | ||
| 142 | + { | ||
| 143 | + return nn_ops.leaky_relu(x, _alpha); | ||
| 144 | + } | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + Tensor negative_part; | ||
| 148 | + if (Math.Abs(_threshold) > 0.000001f) | ||
| 149 | + { | ||
| 150 | + negative_part = gen_ops.relu(-x + _threshold); | ||
| 151 | + } else | ||
| 152 | + { | ||
| 153 | + negative_part = gen_ops.relu(-x + _threshold); | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + if (Math.Abs(_threshold) > 0.000001f) | ||
| 157 | + { | ||
| 158 | + x = x * math_ops.cast(tf.greater(x, _threshold), TF_DataType.TF_FLOAT); | ||
| 159 | + } else if (Math.Abs(_maxValue.Value - 6f) < 0.0001f) | ||
| 160 | + { | ||
| 161 | + x = gen_ops.relu6(x); | ||
| 162 | + } else | ||
| 163 | + { | ||
| 164 | + x = gen_ops.relu(x); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + bool clip_max = _maxValue.HasValue; | ||
| 168 | + if (clip_max) | ||
| 169 | + { | ||
| 170 | + Tensor maxval = constant_op.constant(_maxValue, x.dtype.as_base_dtype()); | ||
| 171 | + var zero = constant_op.constant(0.0f, x.dtype.as_base_dtype()); | ||
| 172 | + x = gen_ops.clip_by_value(x, zero, maxval); | ||
| 173 | + } | ||
| 24 | 174 | ||
| 25 | - var _op = _op_def_lib._apply_op_helper("Relu", name: name, args: new | ||
| 175 | + if (Math.Abs(_alpha) > 0.00001) | ||
| 26 | 176 | { | |
| 27 | - features | ||
| 28 | - }); | ||
| 177 | + var a = constant_op.constant(_alpha, x.dtype.as_base_dtype()); | ||
| 178 | + x -= a * negative_part; | ||
| 179 | + } | ||
| 29 | 180 | ||
| 30 | - return _op.outputs[0]; | ||
| 181 | + return x; | ||
| 182 | + } | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + public class selu : IActivation | ||
| 186 | + { | ||
| 187 | + public Tensor Activate(Tensor x, string name = null) | ||
| 188 | + { | ||
| 189 | + const float alpha = 1.6732632423543772848170429916717f; | ||
| 190 | + const float scale = 1.0507009873554804934193349852946f; | ||
| 191 | + return scale * new elu(alpha).Activate(x, name); | ||
| 192 | + } | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + public class hard_sigmoid : IActivation | ||
| 196 | + { | ||
| 197 | + public Tensor Activate(Tensor x, string name = null) | ||
| 198 | + { | ||
| 199 | + x = (0.2 * x) + 0.5; | ||
| 200 | + var zero = tf.convert_to_tensor(0.0f, x.dtype.as_base_dtype()); | ||
| 201 | + var one = tf.convert_to_tensor(1.0f, x.dtype.as_base_dtype()); | ||
| 202 | + return tf.clip_by_value(x, zero, one); | ||
| 31 | 203 | } | |
| 32 | 204 | } | |
| 33 | - } | ||
| 205 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments