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
|
#!/usr/bin/python3
from sklearn import datasets
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split
import tensorflow as tf
dataset = datasets.load_boston()
data, target = dataset.data, dataset.target
data_scaler = preprocessing.MinMaxScaler()
data = data_scaler.fit_transform(data)
# use sklearn to fit the data values between 0 and 1
x_train, x_test, y_train, y_test = train_test_split(
data, target, train_size=0.9
)
with tf.device('/cpu:0'):
sess = tf.InteractiveSession()
vecInput = tf.placeholder(tf.float32, [13], name="input")
# 'unfitted' means not squeezed between 0 and 1, but real values
numUnfittedTarget = tf.placeholder(tf.float32, [1], name="target")
# numbers to scale the output between 0-1 and back to prices
numFactor = tf.placeholder(tf.float32, [1], name="factor")
numOffset = tf.placeholder(tf.float32, [1], name="offset")
vecBias = tf.Variable(tf.random_uniform([13], minval=0, maxval=1), name="bias")
vecBias = tf.Print(vecBias, [vecBias], "vecbias")
# vecWeights is not a true vector, it is turned by 90 degrees
vecWeights = tf.Variable(tf.random_uniform([1, 13], minval=0, maxval=1), name="weight")
vecWeights = tf.Print(vecWeights, [vecWeights], "vecweights")
# sum((i + b) * w)
matWeighted = tf.mul(tf.add(vecInput, vecBias), vecWeights)
matWeighted = tf.Print(matWeighted, [matWeighted], "matweighted: ") # DEBUG
vecNetinput = tf.reduce_sum(matWeighted, 1)
vecNetinput = tf.Print(vecNetinput, [vecNetinput], "netinput: ") # DEBUG
vecLayerOutput = tf.sigmoid(vecNetinput)
vecLayerOutput = tf.Print(vecLayerOutput, [vecLayerOutput], "vecLayerOutput: ") # DEBUG
# since this is the last layer, vecOutput is a numOutput
numOutput = tf.reduce_sum(vecLayerOutput)
# unscale the output
numUnfittedOutput = tf.add(tf.mul(numOutput, numFactor), numOffset)
numUnfittedOutput = tf.Print(numUnfittedOutput, [numUnfittedOutput], "unfitted output: ") # DEBUG
numUnfittedTarget = tf.Print(numUnfittedTarget, [numUnfittedTarget], "numUnfittedTarget: ") # DEBUG
numDifference = tf.sub(numUnfittedOutput, numUnfittedTarget)
numDifference = tf.Print(numDifference, [numDifference], "numDifference: ")
numMSE = tf.reduce_sum(tf.square(numDifference))
numMSE = tf.Print(numMSE, [numMSE], "MSE: ")
summaryMSE = tf.scalar_summary("MSE", numMSE)
summaryDifference = tf.scalar_summary("Mean difference", numDifference)
summaries = tf.merge_all_summaries()
writer = tf.train.SummaryWriter("/tmp/boston_logs", sess.graph_def)
# all variables have to be specified here
sess.run(tf.initialize_all_variables())
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(numMSE)
factor = max(target) - min(target)
offset = min(target)
for count in range(0, len(x_train)):
trainsteps = 100
print("count " + str(count))
for i in range(0, trainsteps): # 100 epochs
if i % 10 == 9:
feed = {
vecInput: x_test[0],
numUnfittedTarget: [y_test[0]],
numFactor: [factor],
numOffset: [offset]
}
result = sess.run([summaries, numMSE], feed_dict=feed)
#print("test " + result[0])
#writer.add_summary(result[0], count * trainsteps + i)
sess.run(train_step,
feed_dict={
vecInput: x_train[count],
numUnfittedTarget: [y_train[count]],
numFactor: [factor],
numOffset: [offset]
})
print("finished training")
#yt = [y_test]
## debug
#print(" --------- ")
#print("mean difference to test data: ")
#print(sess.run(mse, feed_dict={input_matrix: x_test, real: yt, fact: factor, offs: offset}))
#print(sess.run(diff, feed_dict={input_matrix: x_test, real: yt, fact: factor, offs: offset}))
|