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.

BiLstmCrfNer.cs 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Tensorflow;
  6. namespace TensorFlowNET.Examples
  7. {
  8. /// <summary>
  9. /// Bidirectional LSTM-CRF Models for Sequence Tagging
  10. /// https://github.com/guillaumegenthial/tf_ner/tree/master/models/lstm_crf
  11. /// </summary>
  12. public class BiLstmCrfNer : Python, IExample
  13. {
  14. public int Priority => 13;
  15. public bool Enabled { get; set; } = true;
  16. public bool ImportGraph { get; set; } = false;
  17. public string Name => "bi-LSTM + CRF NER";
  18. HyperParams @params = new HyperParams();
  19. public bool Run()
  20. {
  21. PrepareData();
  22. return true;
  23. }
  24. public void PrepareData()
  25. {
  26. if (!Directory.Exists(HyperParams.DATADIR))
  27. Directory.CreateDirectory(HyperParams.DATADIR);
  28. if (!Directory.Exists(@params.RESULTDIR))
  29. Directory.CreateDirectory(@params.RESULTDIR);
  30. if (!Directory.Exists(@params.MODELDIR))
  31. Directory.CreateDirectory(@params.MODELDIR);
  32. if (!Directory.Exists(@params.EVALDIR))
  33. Directory.CreateDirectory(@params.EVALDIR);
  34. }
  35. private class HyperParams
  36. {
  37. public const string DATADIR = "BiLstmCrfNer";
  38. public string RESULTDIR = Path.Combine(DATADIR, "results");
  39. public string MODELDIR;
  40. public string EVALDIR;
  41. public int dim = 300;
  42. public float dropout = 0.5f;
  43. public int num_oov_buckets = 1;
  44. public int epochs = 25;
  45. public int batch_size = 20;
  46. public int buffer = 15000;
  47. public int lstm_size = 100;
  48. public string words = Path.Combine(DATADIR, "vocab.words.txt");
  49. public string chars = Path.Combine(DATADIR, "vocab.chars.txt");
  50. public string tags = Path.Combine(DATADIR, "vocab.tags.txt");
  51. public string glove = Path.Combine(DATADIR, "glove.npz");
  52. public HyperParams()
  53. {
  54. MODELDIR = Path.Combine(RESULTDIR, "model");
  55. EVALDIR = Path.Combine(MODELDIR, "eval");
  56. }
  57. }
  58. }
  59. }

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