summaryrefslogtreecommitdiff
path: root/boston.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2016-02-19 15:27:21 +0100
committerschneefux <schneefux+commit@schneefux.xyz>2016-02-19 15:27:21 +0100
commit62991425ff6b9fb1dd7014f9d698b1fb53533dd5 (patch)
tree9f91d689da8b88715d2dd92440f7e3d4936c8abd /boston.py
parent18e74fcfe9bc5e0fe799b26bd5029f062adb8f0a (diff)
downloadboston-neuralnet-62991425ff6b9fb1dd7014f9d698b1fb53533dd5.tar.gz
boston-neuralnet-62991425ff6b9fb1dd7014f9d698b1fb53533dd5.zip
clean implementation
Diffstat (limited to 'boston.py')
-rw-r--r--boston.py115
1 files changed, 62 insertions, 53 deletions
diff --git a/boston.py b/boston.py
index a93b489..501ba78 100644
--- a/boston.py
+++ b/boston.py
@@ -3,6 +3,12 @@ from sklearn import datasets
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split
import tensorflow as tf
+import random
+import numpy.random # TODO get rid of these
+
+SEED = 42
+random.seed(SEED)
+numpy.random.seed(SEED)
dataset = datasets.load_boston()
data, target = dataset.data, dataset.target
@@ -17,76 +23,79 @@ 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()
+# TODO weights typc
- numElements = tf.placeholder(tf.int32, [1], name="numelements") # = w(matInputs)=h(vecTargets)
- matInputs = tf.placeholder(tf.float32, [13, None], name="input")
- vecTargets = tf.placeholder(tf.float32, [None], name="target")
+BATCH = 10 # 10 data sets
+STEPS = 100 # 100 epochs
+HIDDEN = 9 # 9 hidden nodes in 1 layer
+IN = 13 # 13 input nodes
+OUT = 1 # 1 output node
- vecBias = tf.Variable(tf.zeros([13]), name="bias")
- vecWeights = tf.Variable(tf.zeros([13]), name="weight")
+with tf.device('/cpu:0'):
+ tf.set_random_seed(SEED)
+ sess = tf.InteractiveSession()
- scalarElements = tf.reshape(numElements, [])
+ with tf.name_scope('input_layer'):
+ matInput = tf.placeholder(tf.float32, [None, IN], name='input')
+ vecTarget = tf.placeholder(tf.float32, [None], name='target')
+ scalarNumelements = tf.shape(vecTarget, name='batch_length')
+ vecBias = tf.Variable(tf.random_uniform([IN], -1.0, 1.0), name='bias')
+ summaryBias = tf.histogram_summary("bias", vecBias)
+ vecsBias = tf.tile(vecBias, scalarNumelements)
+ vecsBias = tf.reshape(vecsBias, [-1, IN])
+ matBiasedInput = tf.add(matInput, vecsBias, name='biased_input')
- # 'vecs' means a (redundant) matrix
- # tile vecBias and vecWeigths, flatten matInputs
- vecsInputs = tf.transpose(matInputs)
- vecsBias = tf.reshape(tf.tile(vecBias, numElements), [13, -1])
- vecsBias = tf.transpose(vecsBias)
- # vecsWeights is not a true vector, it is turned by 90 degrees
- # so that i*w is a matrix
- vecsWeigths = tf.reshape(tf.tile(vecWeights, numElements), [13, -1])
+ with tf.name_scope('hidden_layer'):
+ matWeights = tf.Variable(tf.random_uniform([IN, HIDDEN], -1.0, 1.0),
+ name='weights')
+ summaryWeights = tf.histogram_summary('weigths', matWeights)
+ matLayerin = tf.matmul(matBiasedInput, matWeights, name='layer_input')
+ matLayerout = tf.sigmoid(matLayerin, name='layer_output')
- # now we can sum((i + b) * w)
- matWeighted = tf.mul(tf.add(vecsInputs, vecsBias), vecWeights)
- vecNetinput = tf.reduce_sum(matWeighted, 1)
- vecLayerOutput = tf.sigmoid(vecNetinput)
+ with tf.name_scope('output_layer'):
+ vecTarget = vecTarget
+ matWeigthsHidden = tf.Variable(
+ tf.random_uniform([HIDDEN, OUT], -1.0, 1.0),
+ name='hidden-weigths')
+ summaryWeightsHidden = tf.histogram_summary('hiddenweigths',
+ matWeigthsHidden)
+ # only vec because OUT=1
+ vecLayerinHidden = tf.matmul(matLayerout, matWeigthsHidden,
+ name='hidden_input')
+ vecLayeroutHidden = tf.sigmoid(vecLayerinHidden, name='hidden_output')
+ # TODO in Facharbeit Matrixprodukt erwähnen
- # since this is the last layer, vecOutput is a numOutput
- vecOutput = tf.reduce_sum(vecLayerOutput) # TODO needed?
+ with tf.name_scope('mse'):
+ vecDifference = tf.sub(vecTarget, vecLayeroutHidden, name='diff')
+ vecMSE = tf.square(vecDifference, name='mse')
+ scalarMeanMSE = tf.reduce_mean(vecMSE, name='meanmse')
+ summaryMeanMSE = tf.scalar_summary('MSE', scalarMeanMSE)
- vecDifference = tf.sub(vecOutput, vecTargets)
- vecMSE = tf.square(vecDifference)
- train_step = tf.train.GradientDescentOptimizer(0.01).minimize(tf.reduce_sum(vecMSE))
+ with tf.name_scope('train'):
+ train_step = tf.train.GradientDescentOptimizer(0.1).minimize(
+ scalarMeanMSE)
- # summaries
- # TODO reshape to scalar: reshape(t, [])
- summaryBias = tf.histogram_summary("bias", vecBias)
- summaryWeights = tf.histogram_summary("weigths", vecWeights)
- summaryMSE = tf.scalar_summary("MSE", tf.reduce_sum(vecMSE))
- summaryDifference = tf.scalar_summary("Mean difference", tf.reduce_mean(vecDifference))
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, int(len(x_train) / 10)):
- trainsteps = 100
+ for count in range(0, int(len(x_train) / BATCH)):
print("count " + str(count))
- for i in range(0, trainsteps): # 100 epochs
- #result = sess.run([summaries, numMSE], feed_dict=feed)
- #writer.add_summary(result[0], count * trainsteps + i)
-
- # TODO run a complete set
- xt = x_train[count*10:(count+1)*10]
- xt = [list(i) for i in zip(*xt)] # transpose
+ for i in range(0, STEPS):
result = sess.run([summaries, train_step],
feed_dict={
- numElements: [10],
- matInputs: xt,
- vecTargets: y_train[count*10:(count+1)*10]
+ matInput: x_train[count*BATCH:(count+1)*BATCH],
+ vecTarget: y_train[count*BATCH:(count+1)*BATCH]
})
- if i % 10 == 9:
- writer.add_summary(result[0], count * trainsteps + i) # TODO this slows down
+ writer.add_summary(result[0], count * STEPS + i)
print("finished training")
-# yt = [y_test]
-# # debug
-# print(" --------- ")
-# print("mean difference to test data: ")
-# TODO
+ print("Test results:")
+ result = sess.run([vecDifference],
+ feed_dict={
+ matInput: x_test,
+ vecTarget: y_test
+ })
+ print(result[0])