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.

WordCnn.cs 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System.Collections.Generic;
  14. using Tensorflow;
  15. using static Tensorflow.Python;
  16. namespace TensorFlowNET.Examples.Text
  17. {
  18. public class WordCnn : ITextModel
  19. {
  20. public WordCnn(int vocabulary_size, int document_max_len, int num_class)
  21. {
  22. var embedding_size = 128;
  23. var learning_rate = 0.001f;
  24. var filter_sizes = new int[3, 4, 5];
  25. var num_filters = 100;
  26. var x = tf.placeholder(tf.int32, new TensorShape(-1, document_max_len), name: "x");
  27. var y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y");
  28. var is_training = tf.placeholder(tf.@bool, new TensorShape(), name: "is_training");
  29. var global_step = tf.Variable(0, trainable: false);
  30. var keep_prob = tf.where(is_training, 0.5f, 1.0f);
  31. Tensor x_emb = null;
  32. with(tf.name_scope("embedding"), scope =>
  33. {
  34. var init_embeddings = tf.random_uniform(new int[] { vocabulary_size, embedding_size });
  35. var embeddings = tf.get_variable("embeddings", initializer: init_embeddings);
  36. x_emb = tf.nn.embedding_lookup(embeddings, x);
  37. x_emb = tf.expand_dims(x_emb, -1);
  38. });
  39. var pooled_outputs = new List<Tensor>();
  40. for (int len = 0; len < filter_sizes.Rank; len++)
  41. {
  42. int filter_size = filter_sizes.GetLength(len);
  43. var conv = tf.layers.conv2d(
  44. x_emb,
  45. filters: num_filters,
  46. kernel_size: new int[] { filter_size, embedding_size },
  47. strides: new int[] { 1, 1 },
  48. padding: "VALID",
  49. activation: tf.nn.relu());
  50. var pool = tf.layers.max_pooling2d(
  51. conv,
  52. pool_size: new[] { document_max_len - filter_size + 1, 1 },
  53. strides: new[] { 1, 1 },
  54. padding: "VALID");
  55. pooled_outputs.Add(pool);
  56. }
  57. var h_pool = tf.concat(pooled_outputs, 3);
  58. var h_pool_flat = tf.reshape(h_pool, new TensorShape(-1, num_filters * filter_sizes.Rank));
  59. Tensor h_drop = null;
  60. with(tf.name_scope("dropout"), delegate
  61. {
  62. h_drop = tf.nn.dropout(h_pool_flat, keep_prob);
  63. });
  64. Tensor logits = null;
  65. Tensor predictions = null;
  66. with(tf.name_scope("output"), delegate
  67. {
  68. logits = tf.layers.dense(h_drop, num_class);
  69. predictions = tf.argmax(logits, -1, output_type: tf.int32);
  70. });
  71. with(tf.name_scope("loss"), delegate
  72. {
  73. var sscel = tf.nn.sparse_softmax_cross_entropy_with_logits(logits: logits, labels: y);
  74. var loss = tf.reduce_mean(sscel);
  75. var adam = tf.train.AdamOptimizer(learning_rate);
  76. var optimizer = adam.minimize(loss, global_step: global_step);
  77. });
  78. with(tf.name_scope("accuracy"), delegate
  79. {
  80. var correct_predictions = tf.equal(predictions, y);
  81. var accuracy = tf.reduce_mean(tf.cast(correct_predictions, TF_DataType.TF_FLOAT), name: "accuracy");
  82. });
  83. }
  84. }
  85. }