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.

tf.nn.cs 11 kB

6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. 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.Operations;
  14. using Tensorflow.Operations.Activation;
  15. using static Tensorflow.Binding;
  16. namespace Tensorflow
  17. {
  18. public partial class tensorflow
  19. {
  20. public nn_internal nn { get; } = new nn_internal();
  21. public class nn_internal
  22. {
  23. public Tensor conv2d(Tensor input, Tensor filter, int[] strides, string padding, bool use_cudnn_on_gpu = true,
  24. string data_format = "NHWC", int[] dilations = null, string name = null)
  25. {
  26. return gen_nn_ops.conv2d(input, filter, strides, padding, use_cudnn_on_gpu,
  27. data_format: data_format, dilations: dilations, name: name);
  28. }
  29. public Tensor[] ctc_greedy_decoder(Tensor inputs, Tensor sequence_length, bool merge_repeated = true, string name = null)
  30. => gen_ctc_ops.ctc_greedy_decoder(inputs, sequence_length, merge_repeated: merge_repeated, name: name);
  31. /// <summary>
  32. /// Computes dropout.
  33. /// </summary>
  34. /// <param name="x">A floating point tensor.</param>
  35. /// <param name="keep_prob">(deprecated) A deprecated alias for `(1-rate)`.</param>
  36. /// <param name="noise_shape"></param>
  37. /// <param name="seed">Used to create random seeds.</param>
  38. /// <param name="name"></param>
  39. /// <param name="rate">A scalar `Tensor` with the same type as `x`.</param>
  40. /// <returns>A Tensor of the same shape of `x`.</returns>
  41. public Tensor dropout(Tensor x, Tensor keep_prob = null, Tensor noise_shape = null, int? seed = null, string name = null,
  42. float? rate = null)
  43. {
  44. Tensor keep = null;
  45. if (keep_prob != null)
  46. keep = 1.0f - keep_prob;
  47. var rate_tensor = rate.HasValue ? tf.constant(rate.Value) : keep;
  48. return nn_ops.dropout_v2(x, rate: rate_tensor, noise_shape: noise_shape, seed: seed, name: name);
  49. }
  50. /// <summary>
  51. /// Creates a recurrent neural network specified by RNNCell `cell`.
  52. /// </summary>
  53. /// <param name="cell">An instance of RNNCell.</param>
  54. /// <param name="inputs">The RNN inputs.</param>
  55. /// <param name="dtype"></param>
  56. /// <param name="swap_memory"></param>
  57. /// <param name="time_major"></param>
  58. /// <returns>A pair (outputs, state)</returns>
  59. public (Tensor, Tensor) dynamic_rnn(RnnCell cell, Tensor inputs,
  60. Tensor sequence_length = null, TF_DataType dtype = TF_DataType.DtInvalid,
  61. int? parallel_iterations = null, bool swap_memory = false, bool time_major = false)
  62. => rnn.dynamic_rnn(cell, inputs, sequence_length: sequence_length, dtype: dtype,
  63. parallel_iterations: parallel_iterations, swap_memory: swap_memory,
  64. time_major: time_major);
  65. public Tensor elu(Tensor features, string name = null)
  66. => gen_nn_ops.elu(features, name: name);
  67. public (Tensor, Tensor) moments(Tensor x,
  68. Axis axes,
  69. string name = null,
  70. bool keep_dims = false) => nn_impl.moments(x,
  71. axes,
  72. name: name,
  73. keep_dims: keep_dims);
  74. public Tensor embedding_lookup(IVariableV1 @params,
  75. Tensor ids,
  76. string partition_strategy = "mod",
  77. string name = null) => embedding_ops._embedding_lookup_and_transform(@params,
  78. ids,
  79. partition_strategy: partition_strategy,
  80. name: name);
  81. public Tensor embedding_lookup(Tensor @params,
  82. Tensor ids,
  83. string partition_strategy = "mod",
  84. string name = null) => embedding_ops._embedding_lookup_and_transform(new Tensor[] { @params },
  85. ids,
  86. partition_strategy: partition_strategy,
  87. name: name);
  88. public IActivation relu() => new relu();
  89. public IActivation swish() => new swish();
  90. public IActivation tanh() => new tanh();
  91. public IActivation softmax() => new softmax();
  92. public Tensor tanh(Tensor x, string name = null)
  93. => gen_math_ops.tanh(x, name);
  94. public Tensor relu(Tensor features, string name = null)
  95. => gen_nn_ops.relu(features, name);
  96. public Tensor[] fused_batch_norm(Tensor x,
  97. Tensor scale,
  98. Tensor offset,
  99. Tensor mean = null,
  100. Tensor variance = null,
  101. float epsilon = 0.001f,
  102. string data_format = "NHWC",
  103. bool is_training = true,
  104. string name = null,
  105. float exponential_avg_factor = 1.0f) => nn_impl.fused_batch_norm(x, scale, offset, mean, variance,
  106. epsilon: epsilon,
  107. data_format: data_format,
  108. is_training: is_training,
  109. name: name,
  110. exponential_avg_factor: exponential_avg_factor);
  111. public Tensor max_pool(Tensor value, int[] ksize, int[] strides, string padding, string data_format = "NHWC", string name = null)
  112. => nn_ops.max_pool(value, ksize, strides, padding, data_format: data_format, name: name);
  113. public Tensor in_top_k(Tensor predictions, Tensor targets, int k, string name = "InTopK")
  114. => nn_ops.in_top_k(predictions, targets, k, name);
  115. public Tensor[] top_k(Tensor input, int k = 1, bool sorted = true, string name = null)
  116. => gen_nn_ops.top_kv2(input, k: ops.convert_to_tensor(k), sorted: sorted, name: name);
  117. public Tensor bias_add(Tensor value, IVariableV1 bias, string data_format = null, string name = null)
  118. {
  119. return tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
  120. {
  121. name = scope;
  122. return gen_nn_ops.bias_add(value, ops.convert_to_tensor(bias), data_format: data_format, name: name);
  123. });
  124. }
  125. public Tensor l2_loss(Tensor t, string name = null)
  126. => nn_ops.l2_loss(t, name: name);
  127. /// <summary>
  128. /// Local Response Normalization.
  129. /// </summary>
  130. /// <param name="input"></param>
  131. /// <param name="depth_radius"></param>
  132. /// <param name="bias"></param>
  133. /// <param name="alpha"></param>
  134. /// <param name="beta"></param>
  135. /// <param name="name"></param>
  136. /// <returns></returns>
  137. public Tensor lrn(Tensor input, int depth_radius = 5, int bias = 1,
  138. int alpha = 1, float beta = 0.5f, string name = null)
  139. => gen_nn_ops.lrn(input, depth_radius: depth_radius, bias: bias,
  140. alpha: alpha, beta: beta, name: name);
  141. public Tensor leaky_relu(Tensor features, float alpha = 0.2f, string name = null)
  142. => nn_ops.leaky_relu(features, alpha: alpha, name: name);
  143. public rnn_cell_impl rnn_cell => new rnn_cell_impl();
  144. public Tensor sigmoid_cross_entropy_with_logits(Tensor labels, Tensor logits, string name = null)
  145. => nn_impl.sigmoid_cross_entropy_with_logits(labels: labels, logits: logits, name: name);
  146. public Tensor softmax(Tensor logits, int axis = -1, string name = null)
  147. => gen_nn_ops.softmax(logits, name);
  148. /// <summary>
  149. /// Computes sparse softmax cross entropy between `logits` and `labels`.
  150. /// </summary>
  151. /// <param name="labels"></param>
  152. /// <param name="logits"></param>
  153. /// <param name="name"></param>
  154. /// <returns></returns>
  155. public Tensor sparse_softmax_cross_entropy_with_logits(Tensor labels = null,
  156. Tensor logits = null, string name = null)
  157. => nn_ops.sparse_softmax_cross_entropy_with_logits(labels: labels, logits: logits, name: name);
  158. /// <summary>
  159. /// Computes softmax cross entropy between `logits` and `labels`.
  160. /// </summary>
  161. /// <param name="labels"></param>
  162. /// <param name="logits"></param>
  163. /// <param name="dim"></param>
  164. /// <param name="name"></param>
  165. /// <returns></returns>
  166. public Tensor softmax_cross_entropy_with_logits(Tensor labels, Tensor logits, int dim = -1, string name = null)
  167. {
  168. tf_with(ops.name_scope(name, "softmax_cross_entropy_with_logits_sg", new { logits, labels }), scope =>
  169. {
  170. name = scope;
  171. labels = array_ops.stop_gradient(labels, name: "labels_stop_gradient");
  172. });
  173. return softmax_cross_entropy_with_logits_v2(labels, logits, axis: dim, name: name);
  174. }
  175. public Tensor softmax_cross_entropy_with_logits_v2(Tensor labels, Tensor logits, int axis = -1, string name = null)
  176. => nn_ops.softmax_cross_entropy_with_logits_v2_helper(labels, logits, axis: axis, name: name);
  177. /// <summary>
  178. /// Computes sigmoid of `x` element-wise.
  179. /// Specifically, `y = 1 / (1 + exp(-x))`.
  180. /// </summary>
  181. /// <typeparam name="T"></typeparam>
  182. /// <param name="x"></param>
  183. /// <param name="name">A name for the operation (optional).</param>
  184. /// <returns>A Tensor with the same type as `x`.</returns>
  185. public Tensor sigmoid<T>(T x, string name = null)
  186. => math_ops.sigmoid(x, name: name);
  187. }
  188. }
  189. }