sdm0725 created by HiKat1 - https://repl.it/JmMo/2
#!/usr/bin/env python
# coding:utf-8
#
# use Python3 !!!
#
import math
# import numpy as np
LEAERNING_RATE = 0.1
b1 = 8
b2 = 2
b3 = 0
w11 = (b2 + 1.0) / 10.0
w12 = -0.5
w13 = b1 / 10.0
w14 = -0.3
w15 = -1.0 * b2 / 10.0
w16 = 0.7
w1a = (b3 + 1.0) / 10.0
w1b = -0.9
w1c = -1.0 * (b3 + 1.0) / 10.0
w1d = 0.4
w21 = -1.0 * b1 / 10.0
w22 = -0.6
w23 = -1.0 * b2 / 10.0
w24 = -0.8
w25 = (b1 + 1.0) / 10.0
w26 = 0.2
word_tab2 = ["He", "dressed", "himself"]
def predict_word(inx_lst):
tmp = ""
for x in inx_lst:
tmp = tmp + word_tab2[x] + " "
return tmp
# ReLU
def ReLU(a):
tmp = 0
if a > 0:
tmp = a
return tmp
def softmax_layer(list_a):
sum_accum = 0
for a in list_a:
# print(a)
# print(math.exp(a))
sum_accum += math.exp(a)
# print(sum_accum)
# caution! Python3
return list(map(lambda x: math.exp(x) / sum_accum, list_a))
def My_RNN(x1, x2, x3, h1_pre, h2_pre):
f_h1_pre = ReLU(h1_pre)
f_h2_pre = ReLU(h2_pre)
h1 = x1 * w11 + x2 * w13 + x3 * w15 + f_h1_pre * w1a + f_h2_pre * w1c
h2 = x1 * w12 + x2 * w14 + x3 * w16 + f_h1_pre * w1b + f_h2_pre * w1d
f_h1 = ReLU(h1)
f_h2 = ReLU(h2)
tmp1 = f_h1 * w21 + f_h2 * w24
tmp2 = f_h1 * w22 + f_h2 * w25
tmp3 = f_h1 * w23 + f_h2 * w26
return {"out" : softmax_layer([tmp1, tmp2, tmp3]) , "h1" : h1, "h2" : h2}
# debug
# print(softmax_layer([1,2,3,4]))
print("Answer Q1 ================================")
print('w11 = {0} ,w15 = {1}, w1c = {2}, w23 = {3}'.format(w11, w15, w1c, w23))
print('w13 = {0} ,w1a = {1}, w21 = {2}, w25 = {3}'.format(w13, w1a, w21, w25))
print("Answer Q2 ================================")
q2dic = My_RNN(1, 0, 0, 0, 0)
q2highest = max(q2dic["out"])
h1_t1 = q2dic["h1"]
h2_t1 = q2dic["h2"]
pred_q2 = [i for i,j in enumerate(q2dic["out"]) if j == max(q2dic["out"])]
# print(pred_q2)
# print("debug: network output: {0}".format(q2dic["out"]))
print('The highest score = {0} ,word = {1}'.format(q2highest, predict_word(pred_q2)))
print("Answer Q3 ================================")
q3dic = My_RNN(0, 1, 0, h1_t1, h2_t1)
q3highest = max(q3dic["out"])
h1_t2 = q3dic["h1"]
h2_t2 = q3dic["h2"]
pred_q3 = [i for i,j in enumerate(q3dic["out"]) if j == max(q3dic["out"])]
# print(pred_q3)
# print("debug: network output: {0}".format(q3dic["out"]))
print('The highest score = {0} ,word = {1}'.format(q3highest, predict_word(pred_q3)))
print("Answer Q4 ================================")
q4dic = My_RNN(0, 0, 1, h1_t2, h2_t2)
q4highest = max(q4dic["out"])
pred_q4 = [i for i,j in enumerate(q4dic["out"]) if j == max(q4dic["out"])]
# print(pred_q4)
# print("debug: network output: {0}".format(q4dic["out"]))
print('The highest score = {0} ,word = {1}'.format(q4highest, predict_word(pred_q4)))
# ===============================================