summaryrefslogtreecommitdiff
path: root/boston.py
diff options
context:
space:
mode:
Diffstat (limited to 'boston.py')
-rw-r--r--boston.py89
1 files changed, 38 insertions, 51 deletions
diff --git a/boston.py b/boston.py
index 3a339ab..ede5202 100644
--- a/boston.py
+++ b/boston.py
@@ -18,58 +18,47 @@ x_train, x_test, y_train, y_test = train_test_split(
)
-sess = tf.InteractiveSession()
+with tf.device('/cpu:0'):
+ 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)
+ 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_y_ = tf.div(tf.sub(y_, offs), fact) # input scaled
+ fitted_real = tf.div(tf.sub(real, offs), fact) # input scaled
-W = tf.Variable(tf.zeros([13]), name="weight")
-b = tf.Variable(0.0, name="bias")
+ weights = tf.Variable(tf.truncated_normal([13], stddev=0.1), name="weight")
+ bias = 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
+ 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
-# 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)
+ diff = tf.sub(prediction, real) # for testing
-difference = tf.abs(tf.reduce_sum(tf.sub(y, fitted_y_)))
+ sqr = tf.square(diff)
+ mean = tf.reduce_mean(sqr)
+ mse = mean
-# debug
-#rmse = tf.Print(rmse, [rmse], "rmse: ")
-#
+ difference = tf.abs(
+ tf.reduce_sum(tf.sub(fitted_prediction, fitted_real)))
-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)
+ 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())
+ # all variables have to be specified here
+ sess.run(tf.initialize_all_variables())
-train_step = tf.train.GradientDescentOptimizer(0.005).minimize(rmse)
+ mse = tf.Print(mse, [mse], "mse: ")
+ train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(mse)
last = 0
@@ -90,25 +79,23 @@ while 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
+ for i in range(0, 100): # 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)
+ 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={x: xt, y_: yt, fact: factor, offs: offset})
+ feed_dict={input_matrix: xt, real: 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?
+# 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}))
+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}))