From 47694755a0f66c510e0e9811d5702f493c2b7f71 Mon Sep 17 00:00:00 2001 From: schneefux Date: Thu, 11 Feb 2016 11:18:29 +0100 Subject: geht fast --- boston.py | 59 ++++++++++++++++++++++------------------------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/boston.py b/boston.py index ecf35c2..532b872 100644 --- a/boston.py +++ b/boston.py @@ -9,6 +9,8 @@ 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( @@ -17,44 +19,33 @@ x_train, x_test, y_train, y_test = train_test_split( with tf.device('/cpu:0'): + # TODO use name scopes + sess = tf.InteractiveSession() vecInput = tf.placeholder(tf.float32, [13], name="input") - # 'unfitted' means not squeezed between 0 and 1, but real values - numUnfittedTarget = tf.placeholder(tf.float32, [1], name="target") - - # numbers to scale the output between 0-1 and back to prices - numFactor = tf.placeholder(tf.float32, [1], name="factor") - numOffset = tf.placeholder(tf.float32, [1], name="offset") + numTarget = tf.placeholder(tf.float32, [1], name="target") - vecBias = tf.Variable(tf.random_uniform([13], minval=0, maxval=1), name="bias") - vecBias = tf.Print(vecBias, [vecBias], "vecbias") + vecBias = tf.Variable(tf.zeros([13]), name="bias") # vecWeights is not a true vector, it is turned by 90 degrees - vecWeights = tf.Variable(tf.random_uniform([1, 13], minval=0, maxval=1), name="weight") - vecWeights = tf.Print(vecWeights, [vecWeights], "vecweights") + vecWeights = tf.Variable(tf.zeros([1, 13]), name="weight") # sum((i + b) * w) matWeighted = tf.mul(tf.add(vecInput, vecBias), vecWeights) - matWeighted = tf.Print(matWeighted, [matWeighted], "matweighted: ") # DEBUG vecNetinput = tf.reduce_sum(matWeighted, 1) - vecNetinput = tf.Print(vecNetinput, [vecNetinput], "netinput: ") # DEBUG vecLayerOutput = tf.sigmoid(vecNetinput) - vecLayerOutput = tf.Print(vecLayerOutput, [vecLayerOutput], "vecLayerOutput: ") # DEBUG # since this is the last layer, vecOutput is a numOutput - numOutput = tf.reduce_sum(vecLayerOutput) - # unscale the output - numUnfittedOutput = tf.add(tf.mul(numOutput, numFactor), numOffset) - numUnfittedOutput = tf.Print(numUnfittedOutput, [numUnfittedOutput], "unfitted output: ") # DEBUG - - numUnfittedTarget = tf.Print(numUnfittedTarget, [numUnfittedTarget], "numUnfittedTarget: ") # DEBUG - numDifference = tf.sub(numUnfittedOutput, numUnfittedTarget) - numDifference = tf.Print(numDifference, [numDifference], "numDifference: ") - numMSE = tf.reduce_sum(tf.square(numDifference)) - numMSE = tf.Print(numMSE, [numMSE], "MSE: ") - - summaryMSE = tf.scalar_summary("MSE", numMSE) - summaryDifference = tf.scalar_summary("Mean difference", numDifference) + numOutput = tf.reduce_sum(vecLayerOutput) # TODO needed? + + numDifference = tf.sub(numOutput, numTarget) + numMSE = tf.square(numDifference) + + 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) @@ -63,30 +54,24 @@ with tf.device('/cpu:0'): train_step = tf.train.GradientDescentOptimizer(0.01).minimize(numMSE) -factor = max(target) - min(target) -offset = min(target) - for count in range(0, len(x_train)): trainsteps = 100 print("count " + str(count)) for i in range(0, trainsteps): # 100 epochs if i % 10 == 9: + # TODO mean over test data feed = { vecInput: x_test[0], - numUnfittedTarget: [y_test[0]], - numFactor: [factor], - numOffset: [offset] + numTarget: [y_test[0]] } result = sess.run([summaries, numMSE], feed_dict=feed) - #print("test " + result[0]) - #writer.add_summary(result[0], count * trainsteps + i) + writer.add_summary(result[0], count * trainsteps + i) + # TODO run a complete set sess.run(train_step, feed_dict={ vecInput: x_train[count], - numUnfittedTarget: [y_train[count]], - numFactor: [factor], - numOffset: [offset] + numTarget: [y_train[count]] }) print("finished training") -- cgit v1.3.1