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

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