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.

VdCnn.cs 4.0 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Tensorflow;
  6. namespace TensorFlowNET.Examples.TextClassification
  7. {
  8. public class VdCnn : Python
  9. {
  10. private int embedding_size;
  11. private int[] filter_sizes;
  12. private int[] num_filters;
  13. private int[] num_blocks;
  14. private float learning_rate;
  15. private IInitializer cnn_initializer;
  16. private Tensor x;
  17. private Tensor y;
  18. private Tensor is_training;
  19. private RefVariable global_step;
  20. private RefVariable embeddings;
  21. private Tensor x_emb;
  22. private Tensor x_expanded;
  23. public VdCnn(int alphabet_size, int document_max_len, int num_class)
  24. {
  25. embedding_size = 16;
  26. filter_sizes = new int[] { 3, 3, 3, 3, 3 };
  27. num_filters = new int[] { 64, 64, 128, 256, 512 };
  28. num_blocks = new int[] { 2, 2, 2, 2 };
  29. learning_rate = 0.001f;
  30. cnn_initializer = tf.keras.initializers.he_normal();
  31. x = tf.placeholder(tf.int32, new TensorShape(-1, document_max_len), name: "x");
  32. y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y");
  33. is_training = tf.placeholder(tf.boolean, new TensorShape(), name: "is_training");
  34. global_step = tf.Variable(0, trainable: false);
  35. // Embedding Layer
  36. with(tf.name_scope("embedding"), delegate
  37. {
  38. var init_embeddings = tf.random_uniform(new int[] { alphabet_size, embedding_size }, -1.0f, 1.0f);
  39. embeddings = tf.get_variable("embeddings", initializer: init_embeddings);
  40. x_emb = tf.nn.embedding_lookup(embeddings, x);
  41. x_expanded = tf.expand_dims(x_emb, -1);
  42. });
  43. Tensor conv0 = null;
  44. Tensor conv1 = null;
  45. // First Convolution Layer
  46. with(tf.variable_scope("conv-0"), delegate
  47. {
  48. conv0 = tf.layers.conv2d(x_expanded,
  49. filters: num_filters[0],
  50. kernel_size: new int[] { filter_sizes[0], embedding_size },
  51. kernel_initializer: cnn_initializer,
  52. activation: tf.nn.relu);
  53. conv0 = tf.transpose(conv0, new int[] { 0, 1, 3, 2 });
  54. });
  55. with(tf.name_scope("conv-block-1"), delegate {
  56. conv1 = conv_block(conv0, 1);
  57. });
  58. }
  59. private Tensor conv_block(Tensor input, int i, bool max_pool = true)
  60. {
  61. return with(tf.variable_scope($"conv-block-{i}"), delegate
  62. {
  63. Tensor conv = null;
  64. // Two "conv-batch_norm-relu" layers.
  65. foreach (var j in Enumerable.Range(0, 2))
  66. {
  67. with(tf.variable_scope($"conv-{j}"), delegate
  68. {
  69. // convolution
  70. conv = tf.layers.conv2d(
  71. input,
  72. filters: num_filters[i],
  73. kernel_size: new int[] { filter_sizes[i], num_filters[i - 1] },
  74. kernel_initializer: cnn_initializer,
  75. activation: null);
  76. // batch normalization
  77. conv = tf.layers.batch_normalization(conv, training: is_training);
  78. // relu
  79. conv = tf.nn.relu.Activate(conv);
  80. conv = tf.transpose(conv, new int[] { 0, 1, 3, 2 });
  81. });
  82. }
  83. if (max_pool)
  84. {
  85. // Max pooling
  86. throw new NotImplementedException("conv_block");
  87. }
  88. else
  89. {
  90. return conv;
  91. }
  92. });
  93. }
  94. }
  95. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。