#!/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 ) 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) 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") 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 # 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) difference = tf.abs(tf.reduce_sum(tf.sub(y, fitted_y_))) # debug #rmse = tf.Print(rmse, [rmse], "rmse: ") # 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) 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, 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}) last = next_last print("finished training") #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(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}))