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.

TextClassificationTrain.cs 2.9 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Tensorflow;
  7. using TensorFlowNET.Examples.TextClassification;
  8. using TensorFlowNET.Examples.Utility;
  9. namespace TensorFlowNET.Examples.CnnTextClassification
  10. {
  11. /// <summary>
  12. /// https://github.com/dongjun-Lee/text-classification-models-tf
  13. /// </summary>
  14. public class TextClassificationTrain : Python, IExample
  15. {
  16. public int Priority => 100;
  17. public bool Enabled { get; set; }= false;
  18. public string Name => "Text Classification";
  19. public int? DataLimit = null;
  20. public bool ImportGraph { get; set; } = true;
  21. private string dataDir = "text_classification";
  22. private string dataFileName = "dbpedia_csv.tar.gz";
  23. private const int CHAR_MAX_LEN = 1014;
  24. private const int NUM_CLASS = 2;
  25. public bool Run()
  26. {
  27. PrepareData();
  28. Console.WriteLine("Building dataset...");
  29. var (x, y, alphabet_size) = DataHelpers.build_char_dataset("train", "vdcnn", CHAR_MAX_LEN, DataLimit);
  30. var (train_x, valid_x, train_y, valid_y) = train_test_split(x, y, test_size: 0.15f);
  31. return with(tf.Session(), sess =>
  32. {
  33. new VdCnn(alphabet_size, CHAR_MAX_LEN, NUM_CLASS);
  34. return false;
  35. });
  36. }
  37. private (int[][], int[][], int[], int[]) train_test_split(int[][] x, int[] y, float test_size = 0.3f)
  38. {
  39. int len = x.Length;
  40. int classes = y.Distinct().Count();
  41. int samples = len / classes;
  42. int train_size = int.Parse((samples * (1 - test_size)).ToString());
  43. var train_x = new List<int[]>();
  44. var valid_x = new List<int[]>();
  45. var train_y = new List<int>();
  46. var valid_y = new List<int>();
  47. for (int i = 0; i< classes; i++)
  48. {
  49. for (int j = 0; j < samples; j++)
  50. {
  51. int idx = i * samples + j;
  52. if (idx < train_size + samples * i)
  53. {
  54. train_x.Add(x[idx]);
  55. train_y.Add(y[idx]);
  56. }
  57. else
  58. {
  59. valid_x.Add(x[idx]);
  60. valid_y.Add(y[idx]);
  61. }
  62. }
  63. }
  64. return (train_x.ToArray(), valid_x.ToArray(), train_y.ToArray(), valid_y.ToArray());
  65. }
  66. public void PrepareData()
  67. {
  68. string url = "https://github.com/le-scientifique/torchDatasets/raw/master/dbpedia_csv.tar.gz";
  69. Web.Download(url, dataDir, dataFileName);
  70. Compress.ExtractTGZ(Path.Join(dataDir, dataFileName), dataDir);
  71. }
  72. }
  73. }

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