#!/usr/bin/python3 from sklearn import datasets from sklearn import preprocessing from sklearn.cross_validation import train_test_split import tensorflow as tf dataset = datasets.load_boston() data, target = dataset.data, dataset.target data_scaler = preprocessing.MinMaxScaler() data = data_scaler.fit_transform(data) target_scaler = preprocessing.MinMaxScaler() target = target_scaler.fit_transform(target) # use sklearn to fit the data values between 0 and 1 x_train, x_test, y_train, y_test = train_test_split( data, target, train_size=0.9 ) with tf.device('/cpu:0'): # TODO use name scopes sess = tf.InteractiveSession() vecInput = tf.placeholder(tf.float32, [13], name="input") numTarget = tf.placeholder(tf.float32, [1], name="target") vecBias = tf.Variable(tf.zeros([13]), name="bias") # vecWeights is not a true vector, it is turned by 90 degrees vecWeights = tf.Variable(tf.zeros([1, 13]), name="weight") # sum((i + b) * w) matWeighted = tf.mul(tf.add(vecInput, vecBias), vecWeights) vecNetinput = tf.reduce_sum(matWeighted, 1) vecLayerOutput = tf.sigmoid(vecNetinput) # since this is the last layer, vecOutput is a numOutput numOutput = tf.reduce_sum(vecLayerOutput) # TODO needed? numDifference = tf.sub(numOutput, numTarget) numMSE = tf.square(numDifference) train_step = tf.train.GradientDescentOptimizer(0.01).minimize(numMSE) # summaries summaryBias = tf.histogram_summary("bias", vecBias) summaryWeights = tf.histogram_summary("weigths", vecWeights) summaryDifference = tf.scalar_summary(["difference"], numDifference) summaryMSE = tf.scalar_summary(["MSE"], numMSE) summaryDifference = tf.scalar_summary(["Mean difference"], numDifference) summaries = 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()) for count in range(0, len(x_train)): trainsteps = 100 print("count " + str(count)) for i in range(0, trainsteps): # 100 epochs if False: # if i % 10 == 9: # TODO mean over test data feed = { vecInput: x_test[0], numTarget: [y_test[0]] } result = sess.run([summaries, numMSE], feed_dict=feed) writer.add_summary(result[0], count * trainsteps + i) # TODO run a complete set result = sess.run([summaries, train_step], feed_dict={ vecInput: x_train[count], numTarget: [y_train[count]] }) writer.add_summary(result[0], count * trainsteps + i) # TODO this slows down print("finished training") # yt = [y_test] # # debug # print(" --------- ") # print("mean difference to test data: ") # TODO