summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2016-01-23 19:17:36 +0100
committerschneefux <schneefux+commit@schneefux.xyz>2016-01-23 19:17:36 +0100
commit1a940f3acc003dd234abd50ecefaed6fb47296ee (patch)
tree6753c0b24c412f5169489d99c67ea292e43910f3
parentbd2f24c0924986498bcbf1655ac0bbbb7fb1eb24 (diff)
downloadboston-neuralnet-1a940f3acc003dd234abd50ecefaed6fb47296ee.tar.gz
boston-neuralnet-1a940f3acc003dd234abd50ecefaed6fb47296ee.zip
sieht nicht schlecht aus
-rw-r--r--boston.py97
1 files changed, 63 insertions, 34 deletions
diff --git a/boston.py b/boston.py
index 2491ea5..3a339ab 100644
--- a/boston.py
+++ b/boston.py
@@ -9,77 +9,106 @@ 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
+ data, target, train_size=0.9
)
-sess = tf.InteractiveSession()
+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")
+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")
-sess.run(tf.initialize_all_variables())
-
weighted = tf.mul(x, W)
-combined_weights = tf.reduce_sum(weighted, 1)
+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: ")
+#
-tscal = tf.transpose(tscal)
+mean = tf.reduce_mean(sqr)
+rmse = tf.sqrt(mean)
-scaled_y = tf.add(tf.mul(y, tscal), toffs)
-scaled_y_ = tf.add(tf.mul(y_, tscal), toffs)
+difference = tf.abs(tf.reduce_sum(tf.sub(y, fitted_y_)))
-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
+# debug
+#rmse = tf.Print(rmse, [rmse], "rmse: ")
+#
-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)
+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)
-train_step = tf.train.GradientDescentOptimizer(0.01).minimize(rmsle)
last = 0
-batchsize = 50
+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 _ in y_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})
- 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_))
+#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(rmsle, feed_dict={x: x_test, y_: yt, tscal: target_scale, toffs: target_offset}))
+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}))