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

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

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