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.

VdCnn.cs 1.7 kB

6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tensorflow;
  5. namespace TensorFlowNET.Examples.TextClassification
  6. {
  7. public class VdCnn : Python
  8. {
  9. private int embedding_size;
  10. private int[] filter_sizes;
  11. private int[] num_filters;
  12. private int[] num_blocks;
  13. private float learning_rate;
  14. private IInitializer cnn_initializer;
  15. private Tensor x;
  16. private Tensor y;
  17. private Tensor is_training;
  18. private RefVariable global_step;
  19. private RefVariable embeddings;
  20. private Tensor x_emb;
  21. public VdCnn(int alphabet_size, int document_max_len, int num_class)
  22. {
  23. embedding_size = 16;
  24. filter_sizes = new int[] { 3, 3, 3, 3, 3 };
  25. num_filters = new int[] { 64, 64, 128, 256, 512 };
  26. num_blocks = new int[] { 2, 2, 2, 2 };
  27. learning_rate = 0.001f;
  28. cnn_initializer = tf.keras.initializers.he_normal();
  29. x = tf.placeholder(tf.int32, new TensorShape(-1, document_max_len), name: "x");
  30. y = tf.placeholder(tf.int32, new TensorShape(-1), name: "y");
  31. is_training = tf.placeholder(tf.boolean, new TensorShape(), name: "is_training");
  32. global_step = tf.Variable(0, trainable: false);
  33. with(tf.name_scope("embedding"), delegate
  34. {
  35. var init_embeddings = tf.random_uniform(new int[] { alphabet_size, embedding_size }, -1.0f, 1.0f);
  36. embeddings = tf.get_variable("embeddings", initializer: init_embeddings);
  37. // x_emb = tf.nn.embedding_lookup(embeddings, x);
  38. });
  39. }
  40. }
  41. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。