summaryrefslogtreecommitdiff
path: root/boston.py
blob: 3a339abce308f96ff9af9a64c8821612fdba3c6d (plain)
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
#!/usr/bin/python3
from sklearn import datasets
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split
import numpy as np
import tensorflow as tf

dataset = datasets.load_boston()
data, target = dataset.data, dataset.target

data_scaler = preprocessing.MinMaxScaler()
data = data_scaler.fit_transform(data)

np.random.seed(0)

x_train, x_test, y_train, y_test = train_test_split(
    data, target, train_size=0.9
)


sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, [None, 13], name="input")
y_ = tf.placeholder(tf.float32, [1, None], name="target")
fact = tf.placeholder(tf.float32, [1, None], name="factor")  # max(x) - min(x)
offs = tf.placeholder(tf.float32, [1, None], name="offset")  # min(x)

fitted_y_ = tf.div(tf.sub(y_, offs), fact)  # input scaled

W = tf.Variable(tf.zeros([13]), name="weight")
b = tf.Variable(0.0, name="bias")

weighted = tf.mul(x, W)
combined_weights = tf.reduce_sum(weighted, 1)  # sum w_n*x
y = tf.nn.sigmoid(tf.add(combined_weights, b))
unfitted_y = tf.add(tf.mul(y, fact), offs)  # un-scale prediction

# debug
#unfitted_y = tf.Print(unfitted_y, [unfitted_y], "est: ")
#foo = tf.add(y_, 0)
#foo = tf.Print(foo, [foo], "real: ")
#
# debug
#y = tf.Print(y, [y], "est: ")
#foo = tf.add(fitted_y_, 0)
#fitted_y_ = tf.Print(fitted_y_, [fitted_y_], "real: ")
#

sqr = tf.square(tf.sub(unfitted_y, y_))

# debug
#sqr = tf.Print(sqr, [sqr], "sqr: ")
#

mean = tf.reduce_mean(sqr)
rmse = tf.sqrt(mean)

difference = tf.abs(tf.reduce_sum(tf.sub(y, fitted_y_)))

# debug
#rmse = tf.Print(rmse, [rmse], "rmse: ")
#

rmse_summary = tf.scalar_summary("rmse", rmse)
diff_summary = tf.scalar_summary("diff", difference)
merged = 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.005).minimize(rmse)


last = 0
batchsize = 100
next_last = last + batchsize

factor = [[max(target) - min(target)]]
offset = [[min(target)]]

while next_last < len(x_train):
    print("running next batch")

    next_last = last + batchsize

    if next_last > len(x_train):
        next_last = len(x_train)

    xt = x_train[last:next_last]
    yt = [y_train[last:next_last]]

    for i in range(0, 1000):  # 100 epochs
        if i % 100 == 0:
            feed = {x: x_test, y_: [y_test], fact: factor, offs: offset}
            result = sess.run([merged, rmse, difference], feed_dict=feed)
            writer.add_summary(result[0], i)

        sess.run(train_step,
                 feed_dict={x: xt, y_: yt, fact: factor, offs: offset})

    last = next_last


print("finished training")

#rdiff = tf.reduce_mean(tf.sub(y, y_))
diff = tf.sub(unfitted_y, y_)
yt = [y_test]
# debug?
print(" --------- ")
print("mean difference to test data: ")
print(sess.run(rmse, feed_dict={x: x_test, y_: yt, fact: factor, offs: offset}))
print(sess.run(diff, feed_dict={x: x_test, y_: yt, fact: factor, offs: offset}))