def test_hmm_2States_3Sym_1Char(self): hmm = HMM(2, 3, False) hmm.emission_matrix = [[0.1, 0.5, 0.2, 0.2], [0, 0, 0.5, 0.5]] hmm.initial_matrix = [0.2, 0.8] hmm.transition_matrix = [[0.8, 0.2], [0.8, 0.2]] self.assertAlmostEqual(hmm.calc_probability([0]), 0.0052, 7) self.assertAlmostEqual(hmm.calc_probability([1]), 0.026, 7) self.assertAlmostEqual(hmm.calc_probability([2]), 0.1144, 7) self.assertAlmostEqual(hmm.calc_probability([]), 0.44, 7)
import metric from Models.crf import CRFModel from Models.bilstm import BiLSTM from torch.optim import Adamax import torch import torch.nn.functional as F trainWordLists,trainTagLists,word2id,tag2id=utils.create('train.txt',make_vocab=True) devWordLists,devTagList=utils.create('dev.txt',make_vocab=False) #隐马尔科夫模型训练 print('HMM************************') if os.path.exists('ckpts/hmm.pkl'): hmm=utils.loadModel('ckpts/hmm.pkl') predictTags = hmm.test(devWordLists, word2id, tag2id) else: hmm=HMM(len(tag2id),len(word2id)) hmm.train(trainWordLists,trainTagLists,tag2id,word2id) utils.saveModel('ckpts/hmm.pkl',hmm) predictTags=hmm.test(devWordLists,word2id,tag2id) accuracy=metric.accuracy(predictTags,devTagList) print('accuracy: ',accuracy) print('CRF****************************') # #条件随机序列场模型训练 if os.path.exists('ckpts/crf.pkl'): crf=utils.loadModel('ckpts/crf.pkl') print(crf) predictTags=crf.test(devWordLists) else: crf=CRFModel() crf.train(trainWordLists,trainTagLists) utils.saveModel('ckpts/crf.pkl',crf)
def test_hmm_3States_2Sym_1Char(self): hmm = HMM(3, 2, False) hmm.emission_matrix = [[0.5, 0.3, 0.2], [0, 1, 0], [0.1, 0.1, 0.8]] hmm.initial_matrix = [0.1, 0.2, 0.7] hmm.transition_matrix = [[0, 1, 0], [0.85, 0.1, 0.05], [0.3, 0.2, 0.5]] self.assertAlmostEqual(hmm.calc_probability([0]), 0.0322, 7)