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.

CharCnn.cs 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Tensorflow;
  6. using static Tensorflow.Python;
  7. namespace TensorFlowNET.Examples.Text
  8. {
  9. public class CharCnn : ITextModel
  10. {
  11. public CharCnn(int alphabet_size, int document_max_len, int num_class)
  12. {
  13. var learning_rate = 0.001f;
  14. var filter_sizes = new int[] { 7, 7, 3, 3, 3, 3 };
  15. var num_filters = 256;
  16. var kernel_initializer = tf.truncated_normal_initializer(stddev: 0.05f);
  17. var x = tf.placeholder(tf.int32, new TensorShape(-1, document_max_len), name: "x");
  18. var y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y");
  19. var is_training = tf.placeholder(tf.@bool, new TensorShape(), name: "is_training");
  20. var global_step = tf.Variable(0, trainable: false);
  21. var keep_prob = tf.where(is_training, 0.5f, 1.0f);
  22. var x_one_hot = tf.one_hot(x, alphabet_size);
  23. var x_expanded = tf.expand_dims(x_one_hot, -1);
  24. // ============= Convolutional Layers =============
  25. Tensor pool1 = null, pool2 = null;
  26. Tensor conv3 = null, conv4 = null, conv5 = null, conv6 = null;
  27. Tensor h_pool = null;
  28. with(tf.name_scope("conv-maxpool-1"), delegate
  29. {
  30. var conv1 = tf.layers.conv2d(x_expanded,
  31. filters: num_filters,
  32. kernel_size: new[] { filter_sizes[0], alphabet_size },
  33. kernel_initializer: kernel_initializer,
  34. activation: tf.nn.relu());
  35. pool1 = tf.layers.max_pooling2d(conv1,
  36. pool_size: new[] { 3, 1 },
  37. strides: new[] { 3, 1 });
  38. pool1 = tf.transpose(pool1, new[] { 0, 1, 3, 2 });
  39. });
  40. with(tf.name_scope("conv-maxpool-2"), delegate
  41. {
  42. var conv2 = tf.layers.conv2d(pool1,
  43. filters: num_filters,
  44. kernel_size: new[] {filter_sizes[1], num_filters },
  45. kernel_initializer: kernel_initializer,
  46. activation: tf.nn.relu());
  47. pool2 = tf.layers.max_pooling2d(conv2,
  48. pool_size: new[] { 3, 1 },
  49. strides: new[] { 3, 1 });
  50. pool2 = tf.transpose(pool2, new[] { 0, 1, 3, 2 });
  51. });
  52. with(tf.name_scope("conv-3"), delegate
  53. {
  54. conv3 = tf.layers.conv2d(pool2,
  55. filters: num_filters,
  56. kernel_size: new[] { filter_sizes[2], num_filters },
  57. kernel_initializer: kernel_initializer,
  58. activation: tf.nn.relu());
  59. conv3 = tf.transpose(conv3, new[] { 0, 1, 3, 2 });
  60. });
  61. with(tf.name_scope("conv-4"), delegate
  62. {
  63. conv4 = tf.layers.conv2d(conv3,
  64. filters: num_filters,
  65. kernel_size: new[] { filter_sizes[3], num_filters },
  66. kernel_initializer: kernel_initializer,
  67. activation: tf.nn.relu());
  68. conv4 = tf.transpose(conv4, new[] { 0, 1, 3, 2 });
  69. });
  70. with(tf.name_scope("conv-5"), delegate
  71. {
  72. conv5 = tf.layers.conv2d(conv4,
  73. filters: num_filters,
  74. kernel_size: new[] { filter_sizes[4], num_filters },
  75. kernel_initializer: kernel_initializer,
  76. activation: tf.nn.relu());
  77. conv5 = tf.transpose(conv5, new[] { 0, 1, 3, 2 });
  78. });
  79. with(tf.name_scope("conv-maxpool-6"), delegate
  80. {
  81. conv6 = tf.layers.conv2d(conv5,
  82. filters: num_filters,
  83. kernel_size: new[] { filter_sizes[5], num_filters },
  84. kernel_initializer: kernel_initializer,
  85. activation: tf.nn.relu());
  86. var pool6 = tf.layers.max_pooling2d(conv6,
  87. pool_size: new[] { 3, 1 },
  88. strides: new[] { 3, 1 });
  89. pool6 = tf.transpose(pool6, new[] { 0, 2, 1, 3 });
  90. h_pool = tf.reshape(pool6, new[] { -1, 34 * num_filters });
  91. });
  92. // ============= Fully Connected Layers =============
  93. Tensor fc1_out = null, fc2_out = null;
  94. Tensor logits = null;
  95. Tensor predictions = null;
  96. with(tf.name_scope("fc-1"), delegate
  97. {
  98. fc1_out = tf.layers.dense(h_pool,
  99. 1024,
  100. activation: tf.nn.relu(),
  101. kernel_initializer: kernel_initializer);
  102. });
  103. with(tf.name_scope("fc-2"), delegate
  104. {
  105. fc2_out = tf.layers.dense(fc1_out,
  106. 1024,
  107. activation: tf.nn.relu(),
  108. kernel_initializer: kernel_initializer);
  109. });
  110. with(tf.name_scope("fc-3"), delegate
  111. {
  112. logits = tf.layers.dense(fc2_out,
  113. num_class,
  114. kernel_initializer: kernel_initializer);
  115. predictions = tf.argmax(logits, -1, output_type: tf.int32);
  116. });
  117. with(tf.name_scope("loss"), delegate
  118. {
  119. var y_one_hot = tf.one_hot(y, num_class);
  120. var loss = tf.reduce_mean(
  121. tf.nn.softmax_cross_entropy_with_logits_v2(logits: logits, labels: y_one_hot));
  122. var optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step: global_step);
  123. });
  124. with(tf.name_scope("accuracy"), delegate
  125. {
  126. var correct_predictions = tf.equal(predictions, y);
  127. var accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32), name: "accuracy");
  128. });
  129. }
  130. }
  131. }