#!/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 ) with tf.device('/cpu:0'): sess = tf.InteractiveSession() 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_real = tf.div(tf.sub(real, offs), fact) # input scaled weights = tf.Variable(tf.truncated_normal([13], stddev=0.1), name="weight") bias = tf.Variable(0.0, name="bias") 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 diff = tf.sub(prediction, real) # for testing sqr = tf.square(diff) mean = tf.reduce_mean(sqr) mse = mean difference = tf.abs( tf.reduce_sum(tf.sub(fitted_prediction, fitted_real))) 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()) mse = tf.Print(mse, [mse], "mse: ") train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(mse) 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, 100): # 100 epochs if i % 100 == 0: 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={input_matrix: xt, real: yt, fact: factor, offs: offset}) last = next_last print("finished training") yt = [y_test] # debug print(" --------- ") print("mean difference to test data: ") 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}))