Never Stop Questioning

CRF

最終更新:

t-style

- view
メンバー限定 登録/ログイン
ここを編集
作成途中。

サンプル

#! usr/bin/env python
# encoding=UTF-8

# Copyright (c) 2014 TAJIMA Yoshiyuki 
# All rights reserved.

import random
import math
import scipy as sc
 
GlobalAllY = None
GlobalIndexY = None
 
def Init():
    global GlobalAllY
    global GlobalIndexY
    zo = [0,1]
    GlobalAllY = [sc.array([x, y, z]) for x in zo for y in zo for z in zo]
    i = 0
    GlobalIndexY = {}
    for y in GlobalAllY:
        GlobalIndexY[str(y)] = i
        i += 1
 
def GenData(N):
    S = 0    
    ls = []
    for i in xrange(N):
        if S==0:
            ls.append([1, 0, 0])
        elif S==1:
            ls.append([0, 1, 0])
        elif S==2:
            ls.append([0, 0, 1])
        if random.random()>0.1:
            S = (S + 1) % 3
    return ls
 
def Learn(ls, t, W):
    X = sc.mat(ls[t-3:t])
    Y = sc.mat(ls[t:t+3])[:,0]
    #print X
    #print Y
    
    phils = GenAllPhi(X)
    exp_wphils = GenExp_WPhi(phils, W)
 
    Z = sc.sum([e[1] for e in exp_wphils])
    #print math.exp(W*Phi(X,Y).T) / Z

    numerator = sc.sum([e[0] * e[1] for e in exp_wphils], axis=0)
    sc.add(W, (Phi(X, Y) - numerator / Z))
    W += Phi(X, Y) - numerator / Z
 
def Phi(X, Y):
    ls = []
    for y in Y.flat:
        for x in X.flat:
            if x == 1 and y == 1:
                ls.append(1.0)
            else: 
                ls.append(0.0)
    return sc.mat(ls)
 
 
def IndexY(Y):
    return IndexY[str(Y)]        
 
def GenAllPhi(X):
    phils = [Phi(X, Y) for Y in GlobalAllY]
    return phils
 
def GenExp_WPhi(phils, W):
    ls = map(lambda(phi): [phi, math.exp(W*phi.T)], phils)
    return ls
 
if __name__ == '__main__':
    Init()
 
    N = 35
    ls = GenData(N)
    #print sc.mat(ls).T
    
    W = sc.zeros((1,9*3), sc.double)
    #print W
    for i in xrange(1000):
        t = random.randint(3, N - 3)
        Learn(ls, t, W)
        #print W

 
    X = sc.mat([[1, 0, 0],
                [0, 1, 0],
                [0, 0, 1]])
    phils = GenAllPhi(X)
    exp_wphils = GenExp_WPhi(phils, W)
    Z = sc.sum([e[1] for e in exp_wphils])
    for Y in GlobalAllY:
        print Y, math.exp(W*Phi(X,Y).T) / Z
 
    result = [0, 0]
    for t in xrange(3, N-2):
        X = sc.mat(ls[t-3:t])
        Y = sc.mat(ls[t:t+3])[:,0] 
        phils = GenAllPhi(X)
        exp_wphils = GenExp_WPhi(phils, W)
        Z = sc.sum([e[1] for e in exp_wphils])
        cond = math.exp(W*Phi(X,Y).T) / Z
        if cond>0.05:
            result.append(0)
        else:
            result.append(1)
    print sc.mat(ls).T
    print sc.mat(result)
 
 
 
記事メニュー
目安箱バナー