summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2016-02-11 09:39:53 +0100
committerschneefux <schneefux+commit@schneefux.xyz>2016-02-11 09:39:53 +0100
commitd79e43b4355a8b646073a53485fe08d0273d9966 (patch)
treeaac33683355edbe06bf0c8e6fd3d83bd8945204c
parentcc85180adec2569727652a61b9024ffcb5937fdd (diff)
downloadboston-neuralnet-d79e43b4355a8b646073a53485fe08d0273d9966.tar.gz
boston-neuralnet-d79e43b4355a8b646073a53485fe08d0273d9966.zip
geht (noch) nicht
-rw-r--r--boston.py116
1 files changed, 49 insertions, 67 deletions
diff --git a/boston.py b/boston.py
index 2ee05bd..5996d59 100644
--- a/boston.py
+++ b/boston.py
@@ -2,7 +2,6 @@
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()
@@ -11,8 +10,7 @@ data, target = dataset.data, dataset.target
data_scaler = preprocessing.MinMaxScaler()
data = data_scaler.fit_transform(data)
-np.random.seed(0)
-
+# 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
)
@@ -21,94 +19,78 @@ x_train, x_test, y_train, y_test = train_test_split(
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")
+ vecUnfittedInput = tf.placeholder(tf.float32, [13], name="input")
+ numTarget = 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")
# ^ max(input_matrix) - min(input_matrix)
- offs = tf.placeholder(tf.float32, [1, None], name="offset")
+ numOffset = tf.placeholder(tf.float32, [1], name="offset")
# ^ min(input_matrix)
- fitted_real = tf.div(tf.sub(real, offs), fact) # input scaled
+ # scale the unscaled input
+ vecInput = tf.div(tf.sub(vecUnfittedInput, numOffset), numFactor)
- weights = tf.Variable(tf.truncated_normal([13], stddev=0.1), name="weight")
- bias = tf.Variable(0.0, name="bias")
+ vecBias = tf.Variable(tf.truncated_normal([13], stddev=0.1), name="bias")
+ vecWeights = tf.Variable(tf.truncated_normal([1, 13], stddev=0.1), name="weight")
- 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
+ matWeighted = tf.mul(tf.add(vecInput, vecBias), vecWeights)
+ vecNetinput = tf.reduce_sum(matWeighted)
+ vecNetinput = tf.Print(vecNetinput, [vecNetinput], "netinput: ") # DEBUG
- diff = tf.sub(prediction, real) # for testing
+ vecUnfittedOutput = tf.sigmoid(vecNetinput)
- sqr = tf.square(diff)
- mean = tf.reduce_mean(sqr)
- mse = mean
+ # unscale the output
+ vecOutput = tf.add(tf.mul(vecUnfittedOutput, numFactor), numOffset)
+ # since this is the last layer, vecOutput is a numOutput
+ numOutput = vecOutput
- difference = tf.abs(
- tf.reduce_sum(tf.sub(fitted_prediction, fitted_real)))
+ numDifference = tf.sub(numOutput, numTarget)
+ numMSE = tf.reduce_mean(tf.square(numDifference))
- 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()
+ 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())
# mse = tf.Print(mse, [mse], "mse: ")
- train_step = tf.train.GradientDescentOptimizer(0.01).minimize(mse)
-
-
-last = 0
-batchsize = 100
-next_last = last + batchsize
-
-factor = [[max(target) - min(target)]]
-offset = [[min(target)]]
+ train_step = tf.train.GradientDescentOptimizer(0.01).minimize(numMSE)
-batchcount = 0 # TODO rethink this
-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]]
+factor = 0 #max(target) - min(target)
+offset = 0 #min(target)
+for count in range(0, len(x_train)):
trainsteps = 100
for i in range(0, trainsteps): # 100 epochs
- if i % 10 == 9:
- feed = {
- input_matrix: xt, # x_test,
- real: yt, # [y_test],
- fact: factor,
- offs: offset
- }
- result = sess.run([merged, mse, difference], feed_dict=feed)
- writer.add_summary(result[0], batchcount * trainsteps + i)
+ #if i % 10 == 9:
+ #feed = {
+ #"input": x_test[testcount], # x_test,
+ #"target": y_test[testcount], # [y_test],
+ #"factor": factor,
+ #"offset": offset
+ #}
+ #result = sess.run([summaries, numMSE], feed_dict=feed)
+ #writer.add_summary(result[0], count * trainsteps + i)
+ y_t = 1 #float(y_train[count].tolist())
+ print(y_t)
sess.run(train_step,
feed_dict={
- input_matrix: xt,
- real: yt,
- fact: factor,
- offs: offset
+ "input": 1, #x_train[count].tolist(),
+ "target": 1, #y_t,
+ "factor": 1, #[[factor]],
+ "offset": 1, #[[offset]]
})
- last = next_last
- batchcount += 1
-
-
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}))
+#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}))