summaryrefslogtreecommitdiff
path: root/boston.py
blob: a93b489c605a1e0739ea4857afc71012bffdabf0 (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
#!/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)
target_scaler = preprocessing.MinMaxScaler()
target = target_scaler.fit_transform(target)

# 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'):
    # TODO use name scopes

    sess = tf.InteractiveSession()

    numElements = tf.placeholder(tf.int32, [1], name="numelements")  # = w(matInputs)=h(vecTargets)
    matInputs = tf.placeholder(tf.float32, [13, None], name="input")
    vecTargets = tf.placeholder(tf.float32, [None], name="target")

    vecBias = tf.Variable(tf.zeros([13]), name="bias")
    vecWeights = tf.Variable(tf.zeros([13]), name="weight")

    scalarElements = tf.reshape(numElements, [])

    # 'vecs' means a (redundant) matrix
    # tile vecBias and vecWeigths, flatten matInputs
    vecsInputs = tf.transpose(matInputs)
    vecsBias = tf.reshape(tf.tile(vecBias, numElements), [13, -1])
    vecsBias = tf.transpose(vecsBias)
    # vecsWeights is not a true vector, it is turned by 90 degrees
    # so that i*w is a matrix
    vecsWeigths = tf.reshape(tf.tile(vecWeights, numElements), [13, -1])

    # now we can sum((i + b) * w)
    matWeighted = tf.mul(tf.add(vecsInputs, vecsBias), vecWeights)
    vecNetinput = tf.reduce_sum(matWeighted, 1)
    vecLayerOutput = tf.sigmoid(vecNetinput)

    # since this is the last layer, vecOutput is a numOutput
    vecOutput = tf.reduce_sum(vecLayerOutput)  # TODO needed?

    vecDifference = tf.sub(vecOutput, vecTargets)
    vecMSE = tf.square(vecDifference)
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(tf.reduce_sum(vecMSE))

    # summaries
    # TODO reshape to scalar: reshape(t, [])
    summaryBias = tf.histogram_summary("bias", vecBias)
    summaryWeights = tf.histogram_summary("weigths", vecWeights)
    summaryMSE = tf.scalar_summary("MSE", tf.reduce_sum(vecMSE))
    summaryDifference = tf.scalar_summary("Mean difference", tf.reduce_mean(vecDifference))
    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())

    for count in range(0, int(len(x_train) / 10)):
        trainsteps = 100
        print("count " + str(count))
        for i in range(0, trainsteps):  # 100 epochs
                #result = sess.run([summaries, numMSE], feed_dict=feed)
                #writer.add_summary(result[0], count * trainsteps + i)

            # TODO run a complete set
            xt = x_train[count*10:(count+1)*10]
            xt = [list(i) for i in zip(*xt)]  # transpose
            result = sess.run([summaries, train_step],
                              feed_dict={
                                numElements: [10],
                                matInputs: xt,
                                vecTargets: y_train[count*10:(count+1)*10]
                              })
            if i % 10 == 9:
                writer.add_summary(result[0], count * trainsteps + i)  # TODO this slows down

    print("finished training")

# yt = [y_test]
# # debug
# print(" --------- ")
# print("mean difference to test data: ")
# TODO