-
Notifications
You must be signed in to change notification settings - Fork 15
/
testmlp.py
176 lines (138 loc) · 4.1 KB
/
testmlp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/python
#tensorflow
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
from mpl_toolkits.basemap import Basemap
from matplotlib import colors
from scipy import misc
import glob
import numpy as np
import csv
import matplotlib.pyplot as plt
import time
#setting
learning_rate = 0.05
iterasi = 3000
totvar = 3
hidnode = 5
def batnorm2(matrix):
return np.mean(matrix), np.std(matrix)
def batnorm(matrix):
rata = np.mean(matrix)
variation = np.var(matrix)
#return (matrix - rata) / (variation)**0.5
return (matrix - rata) / np.std(matrix)
####################
#data input session#
####################
#test data
#sintes = np.sin(54)
#costes = np.cos(54)
#tantes = np.tan(54)
#truth
tru = np.zeros((69, 1))
tru[:,0] = range(1,70)
simpantru = range(1,70)
#normalizing truth data
tru[:,0] = batnorm(tru[:,0])
#param
sinus = np.sin(tru[:,0])
cosinus = np.cos(tru[:,0])
tangent = np.tan(tru[:,0])
inputcomp = np.zeros((len(tru), totvar))
for i in xrange(len(tru)):
inputcomp[i,0] = sinus[i]
inputcomp[i,1] = cosinus[i]
inputcomp[i,2] = tangent[i]
#testcomp = np.zeros((len(sintes), totvar))
#for i in xrange(len(sintes)):
# testcomp[i,0] = sintes[i]
# testcomp[i,1] = costes[i]
# testcomp[i,2] = tantes[i]
#####################
#normalizing session#
#####################
#print(inputcomp)
#normalizing training data
contmean = np.zeros(totvar)
contstd = np.zeros(totvar)
for i in xrange(totvar):
contmean[i], contstd[i] = batnorm2(inputcomp[:,i])
inputcomp[:,i] = (inputcomp[:,i] - contmean[i]) / contstd[i]
#for i in xrange(totvar):
# inputcomp[:,i] = batnorm(inputcomp[:,i])
#normalizing test data
#for i in xrange(totvar):
# testcomp[:,i] = batnorm(testcomp[:,i])
# testcomp[:,i] = (testcomp[:,i] - contmean[i]) / contstd[i]
#########################
#operation of tensorflow#
#########################
#bias
#b = tf.Variable(tf.zeros([2]))
#input layer
X = tf.placeholder(tf.float32, [None, totvar])
#W = tf.Variable(tf.zeros([totvar, hidnode]))
W = tf.Variable(tf.random_normal([totvar, hidnode]))
#b = tf.Variable(tf.zeros([hidnode]))
b = tf.Variable(tf.random_normal([hidnode]))
pred = tf.matmul(X, W) + b
#hidden layer
sigm = tf.sigmoid(pred)
#Wh = tf.Variable(tf.zeros([hidnode, 1]))
Wh = tf.Variable(tf.random_normal([hidnode, 1]))
#bh = tf.Variable(tf.zeros([1]))
bh = tf.Variable(tf.random_normal([1]))
finpred = tf.matmul(sigm, Wh) + bh
#hidden layer second
#sigmx = tf.sigmoid(finpred)
#Whx = tf.Variable(tf.random_normal([hidnode, 1]))
#bhx = tf.Variable(tf.random_normal([1]))
#finpredx = tf.matmul(sigm, Whx) + bhx
#true val
Y = tf.placeholder(tf.float32, [None,1])
# Mean squared error
cost = tf.reduce_mean(tf.pow(finpred-Y, 2))
#minimize our lost function
#optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
##################
#training session#
##################
deter = np.zeros((iterasi))
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(iterasi):
sess.run(optimizer, feed_dict={X: inputcomp, Y: tru})
#print(cost.eval(feed_dict={X: inputcomp, Y: tru}))
pre = finpred.eval(feed_dict={X: inputcomp})[:,0]
#deter[i] = np.sqrt((np.mean((pre - tru[:,0])**2.0)))
deter[i] = np.sqrt((np.mean((pre * np.var(simpantru)**0.5 + np.mean(simpantru) - simpantru)**2.0)))
prediksi = finpred.eval(feed_dict={X: inputcomp})[:,0]
second = sigm.eval(feed_dict={X: inputcomp})
wei = Wh.eval()
#################
#predict session#
#################
#prediksitest = finpred.eval(feed_dict={X: testcomp})[:,0]
realpred = prediksi * np.var(simpantru)**0.5 + np.mean(simpantru)
print(realpred)
#print(prediksitest * np.var(simpantru)**0.5 + np.mean(simpantru))
print(np.var(simpantru)**0.5)
##################
#plotting session#
##################
#print(deter)
plt.ylabel('prediksi X')
plt.xlabel('X')
plt.plot(simpantru, realpred)
plt.show()
plt.ylabel('error')
plt.xlabel('iterasi')
plt.plot(range(iterasi), deter)
plt.show()