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 6.7 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 IInitializer fc_initializer;
  17. private Tensor x;
  18. private Tensor y;
  19. private Tensor is_training;
  20. private RefVariable global_step;
  21. private RefVariable embeddings;
  22. private Tensor x_emb;
  23. private Tensor x_expanded;
  24. private Tensor logits;
  25. private Tensor predictions;
  26. private Tensor loss;
  27. public VdCnn(int alphabet_size, int document_max_len, int num_class)
  28. {
  29. embedding_size = 16;
  30. filter_sizes = new int[] { 3, 3, 3, 3, 3 };
  31. num_filters = new int[] { 64, 64, 128, 256, 512 };
  32. num_blocks = new int[] { 2, 2, 2, 2 };
  33. learning_rate = 0.001f;
  34. cnn_initializer = tf.keras.initializers.he_normal();
  35. fc_initializer = tf.truncated_normal_initializer(stddev: 0.05f);
  36. x = tf.placeholder(tf.int32, new TensorShape(-1, document_max_len), name: "x");
  37. y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y");
  38. is_training = tf.placeholder(tf.boolean, new TensorShape(), name: "is_training");
  39. global_step = tf.Variable(0, trainable: false);
  40. // Embedding Layer
  41. with(tf.name_scope("embedding"), delegate
  42. {
  43. var init_embeddings = tf.random_uniform(new int[] { alphabet_size, embedding_size }, -1.0f, 1.0f);
  44. embeddings = tf.get_variable("embeddings", initializer: init_embeddings);
  45. x_emb = tf.nn.embedding_lookup(embeddings, x);
  46. x_expanded = tf.expand_dims(x_emb, -1);
  47. });
  48. Tensor conv0 = null;
  49. Tensor conv1 = null;
  50. Tensor conv2 = null;
  51. Tensor conv3 = null;
  52. Tensor conv4 = null;
  53. Tensor h_flat = null;
  54. Tensor fc1_out = null;
  55. Tensor fc2_out = null;
  56. // First Convolution Layer
  57. with(tf.variable_scope("conv-0"), delegate
  58. {
  59. conv0 = tf.layers.conv2d(x_expanded,
  60. filters: num_filters[0],
  61. kernel_size: new int[] { filter_sizes[0], embedding_size },
  62. kernel_initializer: cnn_initializer,
  63. activation: tf.nn.relu());
  64. conv0 = tf.transpose(conv0, new int[] { 0, 1, 3, 2 });
  65. });
  66. with(tf.name_scope("conv-block-1"), delegate {
  67. conv1 = conv_block(conv0, 1);
  68. });
  69. with(tf.name_scope("conv-block-2"), delegate {
  70. conv2 = conv_block(conv1, 2);
  71. });
  72. with(tf.name_scope("conv-block-3"), delegate {
  73. conv3 = conv_block(conv2, 3);
  74. });
  75. with(tf.name_scope("conv-block-4"), delegate
  76. {
  77. conv4 = conv_block(conv3, 4, max_pool: false);
  78. });
  79. // ============= k-max Pooling =============
  80. with(tf.name_scope("k-max-pooling"), delegate
  81. {
  82. var h = tf.transpose(tf.squeeze(conv4, new int[] { -1 }), new int[] { 0, 2, 1 });
  83. var top_k = tf.nn.top_k(h, k: 8, sorted: false)[0];
  84. h_flat = tf.reshape(top_k, new int[] { -1, 512 * 8 });
  85. });
  86. // ============= Fully Connected Layers =============
  87. with(tf.name_scope("fc-1"), scope =>
  88. {
  89. fc1_out = tf.layers.dense(h_flat, 2048, activation: tf.nn.relu(), kernel_initializer: fc_initializer);
  90. });
  91. with(tf.name_scope("fc-2"), scope =>
  92. {
  93. fc2_out = tf.layers.dense(fc1_out, 2048, activation: tf.nn.relu(), kernel_initializer: fc_initializer);
  94. });
  95. with(tf.name_scope("fc-3"), scope =>
  96. {
  97. logits = tf.layers.dense(fc2_out, num_class, activation: null, kernel_initializer: fc_initializer);
  98. predictions = tf.argmax(logits, -1, output_type: tf.int32);
  99. });
  100. // ============= Loss and Accuracy =============
  101. with(tf.name_scope("loss"), delegate
  102. {
  103. var y_one_hot = tf.one_hot(y, num_class);
  104. loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits: logits, labels: y_one_hot));
  105. var update_ops = tf.get_collection(ops.GraphKeys.UPDATE_OPS) as List<object>;
  106. with(tf.control_dependencies(update_ops.Select(x => (Operation)x).ToArray()), delegate
  107. {
  108. var adam = tf.train.AdamOptimizer(learning_rate);
  109. adam.minimize(loss, global_step: global_step);
  110. });
  111. });
  112. }
  113. private Tensor conv_block(Tensor input, int i, bool max_pool = true)
  114. {
  115. return with(tf.variable_scope($"conv-block-{i}"), delegate
  116. {
  117. Tensor conv = null;
  118. // Two "conv-batch_norm-relu" layers.
  119. foreach (var j in Enumerable.Range(0, 2))
  120. {
  121. with(tf.variable_scope($"conv-{j}"), delegate
  122. {
  123. // convolution
  124. conv = tf.layers.conv2d(
  125. input,
  126. filters: num_filters[i],
  127. kernel_size: new int[] { filter_sizes[i], num_filters[i - 1] },
  128. kernel_initializer: cnn_initializer,
  129. activation: null);
  130. // batch normalization
  131. conv = tf.layers.batch_normalization(conv, training: is_training);
  132. // relu
  133. conv = tf.nn.relu(conv);
  134. conv = tf.transpose(conv, new int[] { 0, 1, 3, 2 });
  135. });
  136. }
  137. if (max_pool)
  138. {
  139. // Max pooling
  140. return tf.layers.max_pooling2d(
  141. conv,
  142. pool_size: new int[] { 3, 1 },
  143. strides: new int[] { 2, 1 },
  144. padding: "SAME");
  145. }
  146. else
  147. {
  148. return conv;
  149. }
  150. });
  151. }
  152. }
  153. }

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