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
|
#!/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()
target_scaler = preprocessing.MinMaxScaler()
print(target)
data = data_scaler.fit_transform(data)
target = target_scaler.fit_transform(target)
target_scale = [target_scaler.inverse_transform([1])] # TODO hack
target_offset = [target_scaler.inverse_transform([0])]
np.random.seed(0)
x_train, x_test, y_train, y_test = train_test_split(
data, target, train_size=0.8
)
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, [None, 13], name="input")
y_ = tf.placeholder(tf.float32, [1, None], name="target")
tscal = tf.placeholder(tf.float32, [1, 1], name="scaler")
toffs = tf.placeholder(tf.float32, [1, 1], name="offset")
W = tf.Variable(tf.zeros([13]), name="weight")
b = tf.Variable(0.0, name="bias")
sess.run(tf.initialize_all_variables())
weighted = tf.mul(x, W)
combined_weights = tf.reduce_sum(weighted, 1)
y = tf.nn.sigmoid(tf.add(combined_weights, b))
tscal = tf.transpose(tscal)
scaled_y = tf.add(tf.mul(y, tscal), toffs)
scaled_y_ = tf.add(tf.mul(y_, tscal), toffs)
foo = tf.add(y_, 0)
foo = tf.Print(foo, [foo], "y_: ")
scaled_y_ = tf.Print(scaled_y_, [scaled_y_], "scaled_y_: ")
scaled_y = tf.add(scaled_y, foo) # delme
diff = tf.square(tf.sub(tf.log(scaled_y + 1), tf.log(scaled_y_ + 1)))
mean = tf.reduce_mean(diff)
sqrt = tf.sqrt(mean)
rmsle = tf.reduce_mean(sqrt)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(rmsle)
last = 0
batchsize = 50
next_last = last + batchsize
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 _ in y_train[last:next_last]]
print("yt " + str(yt[0:3]))
print("tsc " + str(target_scaler.inverse_transform(yt[0:3])))
print("calc " + str([el * target_scale + target_offset for el in yt[0:3]]))
sess.run(train_step, feed_dict={x: xt, y_: yt, tscal: target_scale, toffs: target_offset})
last = next_last
print("finished training")
#diff = tf.reduce_mean(tf.sub(scaled_y, scaled_y_))
yt = [y_test]
print(" --------- ")
print("mean difference to test data: ")
print(sess.run(rmsle, feed_dict={x: x_test, y_: yt, tscal: target_scale, toffs: target_offset}))
|