The following network was trained with the available bank balance at the
time of going to a cafe as input , and the time spent there on that available
balance as output.
The cash balance x was normalised by dividing by 1000 so that the values fell
between 0 and 1. Similarly the time y (minutes) was divided by 100.
The function y= mx + c = 0 was used to measure the gradient of the data.
Algorithm ref: Tensorflow for dummies by Mathew Scarpino, page 109 listing 6.3:
The code from this program can be saved as filename.py
The network can be run from a terminal by typing:
python3 filename.py
Here is the code:
import tensorflow as tf
# values of x = [0.14569, 0.17944, 0.15496, 0.07607, 0.01223, 0.00873, 0.26456, 0.19928, 0.01417, 0.00220, 0.16451, 0.09408, 0.23073, 0.13403, 0.11177, 0.29657, 0.21266, 0.11302, 0.29185, 0.24873, 0.15997, 0.09582, 0.30616, 0.11861, 0.18292, 0.12121, 0.08206]
# values of y = [0.38100, 0.96000, 0.38500, 0.36900, 0.32250, 0.28000, 0.27760, 0.26410, 0.34150, 0.68810, 0.49250, 0.52630, 0.29650, 0.86220, 0.46780, 0.34930, 0.30080, 0.25530, 0.17800, 0.38260, 0.33780, 0.42170, 0.42000, 0.41970, 0.39380, 0.59880, 0.53580]
x_train = [0.14569, 0.17944, 0.15496, 0.07607, 0.01223, 0.00873, 0.26456, 0.19928, 0.01417, 0.00220, 0.16451, 0.09408, 0.23073, 0.13403, 0.11177, 0.29657, 0.21266, 0.11302, 0.29185, 0.24873, 0.15997, 0.09582, 0.30616, 0.11861, 0.18292, 0.12121, 0.08206]
y_train = [0.38100, 0.96000, 0.38500, 0.36900, 0.32250, 0.28000, 0.27760, 0.26410, 0.34150, 0.68810, 0.49250, 0.52630, 0.29650, 0.86220, 0.46780, 0.34930, 0.30080, 0.25530, 0.17800, 0.38260, 0.33780, 0.42170, 0.42000, 0.41970, 0.39380, 0.59880, 0.53580]
m = tf.Variable(0.)
c = tf.Variable(0.)
x = tf.compat.v1.placeholder(dtype=tf.float32)
y = tf.compat.v1.placeholder(dtype=tf.float32)
# using sigmoid function y = mx + c
model = tf.nn.sigmoid(tf.add(tf.multiply(x, m),c))
cost = -1. * tf.reduce_sum(tf.convert_to_tensor(y) * tf.math.log(model) + (1. - tf.convert_to_tensor(y)) * (1. - tf.math.log(model)))
#cost = tf.sigmoid(model)
learn_rate = 0.005
num_epochs = 350
#using Gradient Descent with learning rate 0.01
train = tf.compat.v1.train.GradientDescentOptimizer(learn_rate).minimize(cost)
session = tf.compat.v1.Session()
init = tf.compat.v1.global_variables_initializer()
session.run(init)
#training model for 350 iterations
for epoch in range(num_epochs):
session.run(train, {x:x_train, y:y_train})
#final values of m and c
print('')
print('m =', session.run(m))
print('c =', session.run(c))
Here is the screen output:
david@debian:~/tffin$ python3 sigmoidtensorvii.py
2019-07-17 19:06:31.403105: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 1497180000 Hz
2019-07-17 19:06:31.403556: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55e52606f7a0 executing computations on platform Host. Devices:
2019-07-17 19:06:31.403606: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): <undefined>, <undefined>
2019-07-17 19:06:31.444097: W tensorflow/compiler/jit/mark_for_compilation_pass.cc:1412] (One-time warning): Not using XLA:CPU for cluster because envvar TF_XLA_FLAGS=--tf_xla_cpu_global_jit was not set. If you want XLA:CPU, either set that envvar, or use experimental_jit_scope to enable XLA:CPU. To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a proper command-line flag, not via TF_XLA_FLAGS) or set the envvar XLA_FLAGS=--xla_hlo_profile.
m = -1.1617903
c = -6.003845
david@debian:~/tffin$
For y= m*x + c
as we have a value for m and a value for c, we can calculate the value of X when 0 = mx + c for the gradient at which the neural network starts to learn.
These are the steps:
y = m*x + c
0 = m*x + c
0 = -1.1618x - 6.0039
6.0039 = -1.1618x
x = 6.0039 / -1.1618
x = -5.1667
To scale up after normalising at the start we multiply by 1000
x = -£5166.7
This is the available balance at the gradient of the neural net starting to learn, therefore the balance at which I start to spend is -£5166.7
My available balance ranges from 0 to about £400 maximum. I think the minus value for x indicates that my money is consistent with having received financial gifts in the way of Turkish duty free tobacco, catering sized containers of Nescafe Original, DVDs and yes, cash from my family.
Enjoy!

No comments:
Post a Comment