本文整理汇总了Python中hmm.HMM.learn方法的典型用法代码示例。如果您正苦于以下问题:Python HMM.learn方法的具体用法?Python HMM.learn怎么用?Python HMM.learn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hmm.HMM
的用法示例。
在下文中一共展示了HMM.learn方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import learn [as 别名]
def train(self):
hmmModel = HMM(12, noteRange+1)
hmmModel2 = HMM(12, noteRange+1)
obs = []
ground = []
obs2 = []
ground2 = []
actions = []
for i in range(minNote, maxNote):
actions.append(i)
qModel = QLearner(actions, epsilon=0.1, alpha=0.2, gamma=0.9)
#HMM
#2. It might also be that a lot of four-note runs are produced by the certain class of these short sequences, so the first note of 4-note run, was tried as the hidden state configuration.
#3. Note that a tritone interval sounds much the same anywhere, so it might have been the case that the next note is generated from the difference between the previous note and the note before that.
for ls in self.clusterData:
for quadidx, quad in enumerate(ls):
tempquad = map(lambda x: x - minNote, quad)
obs.append(tempquad[1:])
obs2.append(tempquad[1:])
tempquad2 = map(lambda x: (x - minNote) % 12, quad)
notediff = [tempquad2[0] - tempquad2[1], tempquad2[1] - tempquad2[2], tempquad2[2] - tempquad2[3]]
notediff = map(lambda x: abs(x), notediff)
ground.append(notediff) #difference between prev note and note before that
ground2.append([tempquad2[0]] * 3)
if (quad):
for idx, note in enumerate(quad):
if idx > 0:
prevNote = quad[idx - 1]
qModel.learn(abs((prevNote) - (note)), note, 1, note)
hmmModel.learn(obs, ground)
hmmModel2.learn(obs2, ground2)
return (hmmModel, hmmModel2, qModel)
示例2: train
# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import learn [as 别名]
def train(self):
mmModel = np.zeros((noteRange+1, noteRange+1))
mm3Model = np.zeros((noteRange+1, noteRange+1, noteRange+1))
hmmModel = HMM(12, noteRange+1)
obs = []
ground = []
actions = []
for i in range(minNote, maxNote):
actions.append(i)
qModel = QLearner(actions, epsilon=0.1, alpha=0.2, gamma=0.9)
for ls in self.clusterData:
for quadidx, quad in enumerate(ls):
tempquad = map(lambda x: x - minNote, quad) #take this out for prevnote stuff
obs.append(tempquad[1:]) #this is for hmm: you can also do same thing for qlearning to change state that way
tempquad = map(lambda x: (x - minNote) % 12, quad)
ground.append(tempquad[:3])
if (quad):
for idx, note in enumerate(quad):
if idx > 0:
currNote = note
prevNote = quad[idx - 1]
#Q learning
#q.learn(state1, action1, reward, state2)
qModel.learn(prevNote, note, 1, note)
#Markov model
mmModel[currNote - minNote, prevNote - minNote] += 1
if idx > 2:
#Markov model, more order
currNote = note - minNote
prevNote = quad[idx - 1] - minNote
prevNote2 = quad[idx - 2] - minNote
mm3Model[currNote, prevNote, prevNote2] += 1
hmmModel.learn(obs, ground)
return (mmModel, mm3Model, hmmModel, qModel)
示例3: test_hmm
# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import learn [as 别名]
def test_hmm():
m = HMM(2, 2)
observations = [[0,0,0,0,0,1,1,1,1,1,0,1,0,0,0,1,0,1,1,1,1],[0,0,0,0,1,0,1,1,0,1,1,0,0,1,0,0,1,1,1,1,0,0,1,0,0]]
ground = [[0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1],[0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0]]
m.learn(observations, ground, smooth=None)
trueres = ([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0], -21.944)
res = m.viterbi(observations[1])
assert trueres[0] == res[0]
print trueres[1]
print res[1]
assert abs(trueres[1] - res[1]) < 0.1