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