summaryrefslogtreecommitdiff
path: root/boston.py
blob: ede52028752af6f5023e6567166bc031dc7a622c (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
#!/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
)


with tf.device('/cpu:0'):
    sess = tf.InteractiveSession()

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

    fitted_real = tf.div(tf.sub(real, offs), fact)  # input scaled

    weights = tf.Variable(tf.truncated_normal([13], stddev=0.1), name="weight")
    bias = tf.Variable(0.0, name="bias")

    weighted = tf.reduce_sum(tf.mul(input_matrix, weights), 1)
    # ^ sum ([13]weights*input_matrix)
    fitted_prediction = tf.sigmoid(tf.add(weighted, bias))
    prediction = tf.add(tf.mul(fitted_prediction, fact), offs)
    # ^ un-scale prediction

    diff = tf.sub(prediction, real)  # for testing

    sqr = tf.square(diff)
    mean = tf.reduce_mean(sqr)
    mse = mean

    difference = tf.abs(
        tf.reduce_sum(tf.sub(fitted_prediction, fitted_real)))

    mse_summary = tf.scalar_summary("mse", mse)
    diff_summary = tf.scalar_summary("max diff", tf.reduce_max(diff))
    diff_summary = tf.scalar_summary("mean diff", tf.reduce_mean(diff))
    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())

    mse = tf.Print(mse, [mse], "mse: ")
    train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(mse)


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, 100):  # 100 epochs
        if i % 100 == 0:
            feed = {input_matrix: x_test, real: [y_test], fact: factor, offs: offset}
            result = sess.run([merged, mse, difference], feed_dict=feed)
            writer.add_summary(result[0], i)

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

    last = next_last


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}))