In [1]:
%matplotlib inline
import numpy as np
import os, string
from matplotlib import pyplot as plt
import tensorflow as tf
import seaborn as sns
states = ('Rainy', 'Sunny')
observations = ('walk', 'shop', 'clean')
start_probability = {'Rainy': 0.6, 'Sunny': 0.4}
transition_probability = {
'Rainy' : {'Rainy': 0.7, 'Sunny': 0.3},
'Sunny' : {'Rainy': 0.4, 'Sunny': 0.6},
}
emission_probability = {
'Rainy' : {'walk': 0.1, 'shop': 0.4, 'clean': 0.5},
'Sunny' : {'walk': 0.6, 'shop': 0.3, 'clean': 0.1},
}
stateNum = 2
estateNum = 3
p0=np.mat([0.6,0.4])
T = np.mat([[0.7,0.3],[0.4,0.6]])
E = np.mat([[0.1,0.4,0.5],[0.6,0.3,0.1]])
obchain = np.array([0,1,2])
print(p0)
print(T)
print(E)
In [2]:
with tf.device('/cpu:0'):
with tf.variable_scope("zh", reuse=None):
start = tf.placeholder('float',p0.shape)
#print(start.get_shape())
tT = tf.placeholder('float',T.shape)
tE = tf.placeholder('float',E.shape)
#y = tf.placeholder('float',[obchain.shape[0],estateNum])
y0 = tf.placeholder('float',[1,estateNum])
y1 = tf.placeholder('float',[1,estateNum])
y2 = tf.placeholder('float',[1,estateNum])
state = []
estate = []
pick0 = tf.get_variable(initializer=tf.ones([1,stateNum])/stateNum,name='pick0')
state0 = tf.multiply(start,pick0)#tf.get_variable(initializer=tf.ones([1,stateNum])/stateNum,name='state0')
state0 = tf.minimum(tf.maximum(state0,0.0),1.)
state0 /= tf.reduce_sum(state0)
estate0 = tf.matmul(state0,tE)
pick1 = tf.get_variable(initializer=tf.ones([1,stateNum])/stateNum,name='pick1')
state1 = tf.multiply(tf.matmul(state0,tT),pick1)
state1 = tf.minimum(tf.maximum(state1,0.0),1.)
state1 /= tf.reduce_sum(state1)
estate1 = tf.matmul(state1,tE)
pick2 = tf.get_variable(initializer=tf.ones([1,stateNum])/stateNum,name='pick2')
state2 = tf.multiply(tf.matmul(state1,tT),pick2)
state2 = tf.minimum(tf.maximum(state2,0.0),1.)
state2 /= tf.reduce_sum(state2)
estate2 = tf.matmul(state2,tE)
loss = tf.reduce_sum(tf.multiply(estate0,y0))
loss *= tf.reduce_sum(tf.multiply(estate1,y1))
loss *= tf.reduce_sum(tf.multiply(estate2,y2))
loss *= -1.
global_step = tf.Variable(0, name = 'global_step',trainable=False)
starter_learning_rate = 0.1
learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step,500, 0.95, staircase=True)
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss=loss,global_step = global_step)
init = tf.global_variables_initializer ()
# Launch the graph.
config = tf.ConfigProto(allow_soft_placement = True)
sess = tf.Session(config = config)
sess.run(tf.global_variables_initializer())
In [3]:
import sys,time
y_=np.zeros([obchain.shape[0],estateNum],np.int)
for i in range(0,obchain.shape[0]):
y_[obchain[i],i] = 1
print(y_)
for i in range(1000):
#train
_,epre0,epre1,epre2,curloss,lr = sess.run([train_op,estate0,state1,estate2,loss,learning_rate], feed_dict={y0: y_[0,:].reshape(1,3),y1:y_[1,:].reshape(1,3),y2:y_[2,:].reshape(1,3), tT: T, tE:E,start:p0})
if i%100 == 0:
print(i,epre0,epre1,epre2,curloss,lr)
#sys.stdout.write('\r epoch : %d' % i)
#sys.stdout.flush()
#time.sleep(0.2)
In [5]:
print(sess.run(state0,feed_dict={y0: y_[0,:].reshape(1,3),y1:y_[1,:].reshape(1,3),y2:y_[2,:].reshape(1,3), tT: T, tE:E,start:p0}))
print(sess.run(state1,feed_dict={y0: y_[0,:].reshape(1,3),y1:y_[1,:].reshape(1,3),y2:y_[2,:].reshape(1,3), tT: T, tE:E,start:p0}))
print(sess.run(state2,feed_dict={y0: y_[0,:].reshape(1,3),y1:y_[1,:].reshape(1,3),y2:y_[2,:].reshape(1,3), tT: T, tE:E,start:p0}))
#print(sess.run(latentChainProb[2]))
In [8]:
help(tf.concat)
In [5]:
a=np.matmul(np.multiply(p0,T),E)
print(a)
print(a.sum(axis=0))
In [6]:
print(0.282 + 0.372 + 0.366)
In [ ]: