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

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