The following network was trained with the available bank balance at the time of going to a cafe, and the time spent there on that available balance.
The balance was normalised by dividing by 1000 so that the values fell between 0 and 1. Similarly the time (minutes) was divided by 100.
The code from this program can be saved as filename.py
The network can be run from a terminal by typing:
python filename.py
Here is the code:
from numpy import exp, array, random, dot
import numpy as np
class NeuralNetwork():
def __init__(self):
# Seed the random number generator, so it generates the same numbers
# every time the program runs.
np.random.seed(1)
# We model a single neuron, with 1 input connection and 1 output connection.
# We assign random weights to a 1 x 1 matrix, with values in the range -1 to 1
# and mean 0.
self.synaptic_weights = np.random.random((1, 1)) - 1
# The Sigmoid function, which describes an S shaped curve.
# We pass the weighted sum of the inputs through this function to
# normalise them between 0 and 1.
def sigmoid(self, x):
return 1 / (1 + exp(-x))
# The derivative of the Sigmoid function.
# This is the gradient of the Sigmoid curve.
# It indicates how confident we are about the existing weight.
def sigmoid_derivative(self, x):
return x * (1 - x)
# We train the neural network through a process of trial and error.
# Adjusting the synaptic weights each time.
def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
for iteration in xrange(number_of_training_iterations):
# Pass the training set through our neural network (a single neuron).
output = self.think(training_set_inputs)
# Calculate the error (The difference between the desired output
# and the predicted output).
error = training_set_outputs - output
# Multiply the error by the input and again by the gradient of the Sigmoid curve.
# This means less confident weights are adjusted more.
# This means inputs, which are zero, do not cause changes to the weights.
adjustment = np.dot(training_set_inputs.T, error * self.sigmoid_derivative(output))
# Adjust the weights.
self.synaptic_weights += adjustment
# The neural network thinks.
def think(self, inputs):
inputs = inputs.astype(float)
# Pass inputs through our neural network (our single neuron).
output = self.sigmoid(np.dot(inputs, self.synaptic_weights))
return output
# return self.__sigmoid(np.dot(training_set_inputs, self.synaptic_weights))
if __name__ == "__main__":
#Intialise a single neuron neural network.
neural_network = NeuralNetwork()
training_set_inputs = np.array([[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 ]])
#training_set_inputs = np.array(training_set_inputs, ndmin = 2).T
print(training_set_inputs, training_set_inputs.shape)
#training_set_outputs = np.array([[0.51050, 0.50950, 0.50750, 0.510, 0.5110, 0.50250, 0.50450, 0.5090, 0.50750, 0.5050, 0.503, 0.5065, 0.50750, 0.505, 0.503, 0.50650, 0.507, 0.5050, 0.49420, 0.5070, 0.499, 0.49330, 0.5095, 0.51650, 0.501, 0.51150]]).T
training_set_outputs = np.array([[0.381, 0.96, 0.385, 0.369, 0.3225, 0.28, 0.2776, 0.2641, 0.3415, 0.6881, 0.4925, 0.5263, 0.2965, 0.8622, 0.4678, 0.3493, 0.3008, 0.2553, 0.178, 0.3826, 0.3378, 0.4217, 0.42, 0.4197, 0.3938, 0.5988, 0.5358]]).T
#training_set_outputs = np.array(training_set_outputs, ndmin = 1).T
print(training_set_outputs, training_set_outputs.shape)
print "Random starting synaptic weights: "
print neural_network.synaptic_weights
# Train the neural network using a training set.
# Do it 10,000 times and make small adjustments each time.
neural_network.train(training_set_inputs, training_set_outputs, 10000)
print "New synaptic weights after training: "
print neural_network.synaptic_weights
user_input_one = str(input("User Input: "))
# Test the neural network with a new situation.
print("Considering new situation of available balance [input value] -> ?: ", user_input_one)
print(neural_network.think(np.array([user_input_one])))# enter some input value inside brackets
The screen output is as follows:
david@debian:~/neuralnetworks/Scikitnumpy$ python hobtimepredict.py
(array([[ 0.14569],
[ 0.17944],
[ 0.15496],
[ 0.07607],
[ 0.01223],
[ 0.00873],
[ 0.26456],
[ 0.19928],
[ 0.01417],
[ 0.0022 ],
[ 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]]), (27, 1))
(array([[ 0.381 ],
[ 0.96 ],
[ 0.385 ],
[ 0.369 ],
[ 0.3225],
[ 0.28 ],
[ 0.2776],
[ 0.2641],
[ 0.3415],
[ 0.6881],
[ 0.4925],
[ 0.5263],
[ 0.2965],
[ 0.8622],
[ 0.4678],
[ 0.3493],
[ 0.3008],
[ 0.2553],
[ 0.178 ],
[ 0.3826],
[ 0.3378],
[ 0.4217],
[ 0.42 ],
[ 0.4197],
[ 0.3938],
[ 0.5988],
[ 0.5358]]), (27, 1))
Random starting synaptic weights:
[[-0.582978]]
New synaptic weights after training:
[[-1.93043177]]
User Input: 0.23223
('Considering new situation of available balance [input value] -> ?: ', '0.23223')
[ 0.38976404]
david@debian:~/neuralnetworks/Scikitnumpy$
The new case of available balance to input is first divided by 1000, so £232.22 becomes 0.23223 to input.
The resulting output time is multiplied by 100 to give 38.976404 minutes.
Enjoy!!
Sunday, 30 June 2019
Wednesday, 26 June 2019
Neural network to work out cashflow and timeflow
The following neural network in Python was trained on available cashflow in my bank account prior to the time of purchase of a coffee and snack at a set location. The time spent at this activity at the cafe was measured using a stopwatch. To normalise the data used in training, the available balances were divided by a 1000 and minutes were divided by 100 so all the values fell between 0 and 1. So after running the network, the predicted output for cashflow is multiplied by 1000 and the minutes multiplied by 100. Predicted X for this example is 0.369 * 1000 = £369 predicted available cashflow on approach to the cafe, and the predicted Y is 0.42 * 100 = 42 minutes predicted time spent there.
After saving the code to a file filename.py it can be run from a terminal by typing python filename.py
Here is the code:
import numpy as np
from numpy import array
from sklearn import svm
clf = svm.SVC(gamma=0.001, C=100)
y = np.array([0.381, 0.96, 0.385, 0.369, 0.3225, 0.28, 0.2776, 0.2641, 0.3415,
0.6881, 0.4925, 0.5263, 0.2965, 0.8622, 0.4678, 0.3493, 0.3008, 0.2553, 0.178,
0.3826, 0.3378, 0.4217, 0.42, 0.4197, 0.3938, 0.5988, 0.5358]).T
y = np.array(y, ndmin = 1).T
print(y, y.shape)
x = np.array([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 ]).T
x = np.array(x, ndmin = 2).T
print(x, x.shape)
xtrain, ytrain = x[:-1], y[:-1]
clf.fit(xtrain, ytrain) #train the data
print('Prediction for x:', clf.predict(x[-1])) #predict the data
print('Prediction for y:', clf.predict(y[-1])) #predict the data
Here is the screen / terminal output:
david@debian:~/Timecfnn$ python cashflowtime+time.py
(array([ 0.381 , 0.96 , 0.385 , 0.369 , 0.3225, 0.28 , 0.2776,
0.2641, 0.3415, 0.6881, 0.4925, 0.5263, 0.2965, 0.8622,
0.4678, 0.3493, 0.3008, 0.2553, 0.178 , 0.3826, 0.3378,
0.4217, 0.42 , 0.4197, 0.3938, 0.5988, 0.5358]), (27,))
(array([[ 0.14569],
[ 0.17944],
[ 0.15496],
[ 0.07607],
[ 0.01223],
[ 0.00873],
[ 0.26456],
[ 0.19928],
[ 0.01417],
[ 0.0022 ],
[ 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]]), (27, 1))
('Prediction for x:', array([ 0.369]))
('Prediction for y:', array([ 0.42]))
david@debian:~/Timecfnn$
Acknowledgements: I would like to thank Jack's Snax of Barum Arcade in Barnstaple for the endless supply of coffee, and houmous and haloumi wraps with lettuce and tomato and pleasant conversation.
Picture: me outside Jack's Snax
After saving the code to a file filename.py it can be run from a terminal by typing python filename.py
Here is the code:
import numpy as np
from numpy import array
from sklearn import svm
clf = svm.SVC(gamma=0.001, C=100)
y = np.array([0.381, 0.96, 0.385, 0.369, 0.3225, 0.28, 0.2776, 0.2641, 0.3415,
0.6881, 0.4925, 0.5263, 0.2965, 0.8622, 0.4678, 0.3493, 0.3008, 0.2553, 0.178,
0.3826, 0.3378, 0.4217, 0.42, 0.4197, 0.3938, 0.5988, 0.5358]).T
y = np.array(y, ndmin = 1).T
print(y, y.shape)
x = np.array([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 ]).T
x = np.array(x, ndmin = 2).T
print(x, x.shape)
xtrain, ytrain = x[:-1], y[:-1]
clf.fit(xtrain, ytrain) #train the data
print('Prediction for x:', clf.predict(x[-1])) #predict the data
print('Prediction for y:', clf.predict(y[-1])) #predict the data
Here is the screen / terminal output:
david@debian:~/Timecfnn$ python cashflowtime+time.py
(array([ 0.381 , 0.96 , 0.385 , 0.369 , 0.3225, 0.28 , 0.2776,
0.2641, 0.3415, 0.6881, 0.4925, 0.5263, 0.2965, 0.8622,
0.4678, 0.3493, 0.3008, 0.2553, 0.178 , 0.3826, 0.3378,
0.4217, 0.42 , 0.4197, 0.3938, 0.5988, 0.5358]), (27,))
(array([[ 0.14569],
[ 0.17944],
[ 0.15496],
[ 0.07607],
[ 0.01223],
[ 0.00873],
[ 0.26456],
[ 0.19928],
[ 0.01417],
[ 0.0022 ],
[ 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]]), (27, 1))
('Prediction for x:', array([ 0.369]))
('Prediction for y:', array([ 0.42]))
david@debian:~/Timecfnn$
Acknowledgements: I would like to thank Jack's Snax of Barum Arcade in Barnstaple for the endless supply of coffee, and houmous and haloumi wraps with lettuce and tomato and pleasant conversation.
Picture: me outside Jack's Snax
Tuesday, 12 February 2019
Sigmoid activation function
In general, a sigmoid function is real-valued and differentiable, having a non-negative or non-positive first derivative, one local minimum, and one local maximum.
Sigmoid functions are often used in artificial neural networks to introduce nonlinearity in the model.
A neural network element computes a linear combination of its input signals, and applies a sigmoid function to the result. A reason for its popularity in neural networks is because the sigmoid function satisfies a property between the derivative and itself such that it is computationally easy to perform.
Derivatives of the sigmoid function are usually employed in learning algorithms
REF: https://excel.ucf.edu/classes/2007/Spring/appsII/Chapter1.pdf
Sigmoid function produces similar results to step function in that the output is between 0 and 1. The curve crosses 0.5 at z=0, which we can set up rules for the activation function, such as: If the sigmoid neuron’s output is larger than or equal to 0.5, it outputs 1; if the output is smaller than 0.5, it outputs 0.
Sigmoid function does not have a jerk on its curve. It is smooth and it has a very nice and simple derivative of σ(z) * (1-σ(z)), which is differentiable everywhere on the curve.
REF: https://towardsdatascience.com/multi-layer-neural-networks-with-sigmoid-function-deep-learning-for-rookies-2-bf464f09eb7f
Many natural processes, such as those of complex system learning curves, exhibit a progression from small beginnings that accelerates and approaches a climax over time. When a specific mathematical model is lacking, a sigmoid function is often used.
REF: https://en.wikipedia.org/wiki/Sigmoid_function
The following is Python code to implement a sigmoid activation function
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1 / (1 + np.e**(-x))
x = np.linspace(-10, 10, 1000)
y = sigmoid(x)
plt.xlabel('$x$', fontsize=22); plt.ylabel('$y = 1 / (1 + np.e**(-x))$', fontsize=22)
plt.plot(x, y,label = 'Sigmoid curve')
plt.legend(loc='upper left')
plt.plot(0.0, sigmoid(0), 'r.')
plt.yticks([0, 0.25, 0.5, 0.75, 1])
plt.show()
REF: https://datascience.stackexchange.com/questions/30676/role-derivative-of-sigmoid-function-in-neural-networks
Sigmoid functions are often used in artificial neural networks to introduce nonlinearity in the model.
A neural network element computes a linear combination of its input signals, and applies a sigmoid function to the result. A reason for its popularity in neural networks is because the sigmoid function satisfies a property between the derivative and itself such that it is computationally easy to perform.
Derivatives of the sigmoid function are usually employed in learning algorithms
REF: https://excel.ucf.edu/classes/2007/Spring/appsII/Chapter1.pdf
Sigmoid function produces similar results to step function in that the output is between 0 and 1. The curve crosses 0.5 at z=0, which we can set up rules for the activation function, such as: If the sigmoid neuron’s output is larger than or equal to 0.5, it outputs 1; if the output is smaller than 0.5, it outputs 0.
Sigmoid function does not have a jerk on its curve. It is smooth and it has a very nice and simple derivative of σ(z) * (1-σ(z)), which is differentiable everywhere on the curve.
REF: https://towardsdatascience.com/multi-layer-neural-networks-with-sigmoid-function-deep-learning-for-rookies-2-bf464f09eb7f
Many natural processes, such as those of complex system learning curves, exhibit a progression from small beginnings that accelerates and approaches a climax over time. When a specific mathematical model is lacking, a sigmoid function is often used.
REF: https://en.wikipedia.org/wiki/Sigmoid_function
The following is Python code to implement a sigmoid activation function
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1 / (1 + np.e**(-x))
x = np.linspace(-10, 10, 1000)
y = sigmoid(x)
plt.xlabel('$x$', fontsize=22); plt.ylabel('$y = 1 / (1 + np.e**(-x))$', fontsize=22)
plt.plot(x, y,label = 'Sigmoid curve')
plt.legend(loc='upper left')
plt.plot(0.0, sigmoid(0), 'r.')
plt.yticks([0, 0.25, 0.5, 0.75, 1])
plt.show()
REF: https://datascience.stackexchange.com/questions/30676/role-derivative-of-sigmoid-function-in-neural-networks
Sunday, 3 February 2019
Python prediction two
This is my second attempt at implementing a neural net in Python to
predict a closing price of Post Office shares after training the net on
opening and closing prices.
The algorithm came from
https://houseofbots.com/news-detail/4242-1-learn-how-to-build-a-simple-neural-network-in-9-lines-of-python-code
and
https://www.kdnuggets.com/2018/10/simple-neural-network-python.html
Here is my code:
from numpy import exp, array, random, dot
import numpy as np
class NeuralNetwork():
def __init__(self):
# Seed the random number generator, so it generates the same numbers
# every time the program runs.
np.random.seed(1)
# We model a single neuron, with 3 input connections and 1 output connection.
# We assign random weights to a 3 x 1 matrix, with values in the range -1 to 1
# and mean 0.
self.synaptic_weights = 2 * np.random.random((1, 1)) - 1
# The Sigmoid function, which describes an S shaped curve.
# We pass the weighted sum of the inputs through this function to
# normalise them between 0 and 1.
def sigmoid(self, x):
return 1 / (1 + exp(-x))
# The derivative of the Sigmoid function.
# This is the gradient of the Sigmoid curve.
# It indicates how confident we are about the existing weight.
def sigmoid_derivative(self, x):
return x * (1 - x)
# We train the neural network through a process of trial and error.
# Adjusting the synaptic weights each time.
def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
for iteration in xrange(number_of_training_iterations):
# Pass the training set through our neural network (a single neuron).
output = self.think(training_set_inputs)
# Calculate the error (The difference between the desired output
# and the predicted output).
error = training_set_outputs - output
# Multiply the error by the input and again by the gradient of the Sigmoid curve.
# This means less confident weights are adjusted more.
# This means inputs, which are zero, do not cause changes to the weights.
adjustment = np.dot(training_set_inputs.T, error * self.sigmoid_derivative(output))
# Adjust the weights.
self.synaptic_weights += adjustment
# The neural network thinks.
def think(self, inputs):
inputs = inputs.astype(float)
# Pass inputs through our neural network (our single neuron).
output = self.sigmoid(np.dot(inputs, self.synaptic_weights))
return output
# return self.__sigmoid(np.dot(training_set_inputs, self.synaptic_weights))
if __name__ == "__main__":
#Intialise a single neuron neural network.
neural_network = NeuralNetwork()
training_set_inputs = np.array([[0.512],
[0.514],
[0.509],
[0.508],
[0.510],
[0.50750],
[0.513],
[0.50650],
[0.50050],
[0.51050],
[0.503],
[0.505],
[0.510],
[0.5065],
[0.5165],
[0.5175],
[0.522],
[0.5145],
[0.5135],
[0.5225],
[0.525],
[0.525],
[0.525],
[0.5245],
[0.508],
[0.524]])
print(training_set_inputs, training_set_inputs.shape)
training_set_outputs = np.array([[0.51050, 0.50950, 0.50750, 0.510, 0.5110, 0.50250, 0.50450, 0.5090, 0.50750, 0.5050, 0.503, 0.5065, 0.50750, 0.505, 0.503, 0.50650, 0.507, 0.5050, 0.49420, 0.5070, 0.499, 0.49330, 0.5095, 0.51650, 0.501, 0.51150]]).T
print(training_set_outputs, training_set_outputs.shape)
print "Random starting synaptic weights: "
print neural_network.synaptic_weights
# Train the neural network using a training set.
# Do it 10,000 times and make small adjustments each time.
neural_network.train(training_set_inputs, training_set_outputs, 10000)
print "New synaptic weights after training: "
print neural_network.synaptic_weights
user_input_one = str(input("User Input One: "))
# Test the neural network with a new situation.
print("Considering new situation [some input value] -> ?: ", user_input_one)
print(neural_network.think(np.array([user_input_one])))# enter some input value inside brackets
The neural network can be run from a terminal by the command
python myneuralnetcode.py
The screen output after running the neural network is
(array([[ 0.512 ],
[ 0.514 ],
[ 0.509 ],
[ 0.508 ],
[ 0.51 ],
[ 0.5075],
[ 0.513 ],
[ 0.5065],
[ 0.5005],
[ 0.5105],
[ 0.503 ],
[ 0.505 ],
[ 0.51 ],
[ 0.5065],
[ 0.5165],
[ 0.5175],
[ 0.522 ],
[ 0.5145],
[ 0.5135],
[ 0.5225],
[ 0.525 ],
[ 0.525 ],
[ 0.525 ],
[ 0.5245],
[ 0.508 ],
[ 0.524 ]]), (26, 1))
(array([[ 0.5105],
[ 0.5095],
[ 0.5075],
[ 0.51 ],
[ 0.511 ],
[ 0.5025],
[ 0.5045],
[ 0.509 ],
[ 0.5075],
[ 0.505 ],
[ 0.503 ],
[ 0.5065],
[ 0.5075],
[ 0.505 ],
[ 0.503 ],
[ 0.5065],
[ 0.507 ],
[ 0.505 ],
[ 0.4942],
[ 0.507 ],
[ 0.499 ],
[ 0.4933],
[ 0.5095],
[ 0.5165],
[ 0.501 ],
[ 0.5115]]), (26, 1))
Random starting synaptic weights:
[[-0.16595599]]
New synaptic weights after training:
[[ 0.04566562]]
User Input One: 0.508
('Considering new situation [some input value] -> ?: ', '0.508')
[ 0.50579927]
david@debian:~/neuralnetworks/Scikitnumpy$
The algorithm came from
https://houseofbots.com/news-detail/4242-1-learn-how-to-build-a-simple-neural-network-in-9-lines-of-python-code
and
https://www.kdnuggets.com/2018/10/simple-neural-network-python.html
Here is my code:
from numpy import exp, array, random, dot
import numpy as np
class NeuralNetwork():
def __init__(self):
# Seed the random number generator, so it generates the same numbers
# every time the program runs.
np.random.seed(1)
# We model a single neuron, with 3 input connections and 1 output connection.
# We assign random weights to a 3 x 1 matrix, with values in the range -1 to 1
# and mean 0.
self.synaptic_weights = 2 * np.random.random((1, 1)) - 1
# The Sigmoid function, which describes an S shaped curve.
# We pass the weighted sum of the inputs through this function to
# normalise them between 0 and 1.
def sigmoid(self, x):
return 1 / (1 + exp(-x))
# The derivative of the Sigmoid function.
# This is the gradient of the Sigmoid curve.
# It indicates how confident we are about the existing weight.
def sigmoid_derivative(self, x):
return x * (1 - x)
# We train the neural network through a process of trial and error.
# Adjusting the synaptic weights each time.
def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
for iteration in xrange(number_of_training_iterations):
# Pass the training set through our neural network (a single neuron).
output = self.think(training_set_inputs)
# Calculate the error (The difference between the desired output
# and the predicted output).
error = training_set_outputs - output
# Multiply the error by the input and again by the gradient of the Sigmoid curve.
# This means less confident weights are adjusted more.
# This means inputs, which are zero, do not cause changes to the weights.
adjustment = np.dot(training_set_inputs.T, error * self.sigmoid_derivative(output))
# Adjust the weights.
self.synaptic_weights += adjustment
# The neural network thinks.
def think(self, inputs):
inputs = inputs.astype(float)
# Pass inputs through our neural network (our single neuron).
output = self.sigmoid(np.dot(inputs, self.synaptic_weights))
return output
# return self.__sigmoid(np.dot(training_set_inputs, self.synaptic_weights))
if __name__ == "__main__":
#Intialise a single neuron neural network.
neural_network = NeuralNetwork()
training_set_inputs = np.array([[0.512],
[0.514],
[0.509],
[0.508],
[0.510],
[0.50750],
[0.513],
[0.50650],
[0.50050],
[0.51050],
[0.503],
[0.505],
[0.510],
[0.5065],
[0.5165],
[0.5175],
[0.522],
[0.5145],
[0.5135],
[0.5225],
[0.525],
[0.525],
[0.525],
[0.5245],
[0.508],
[0.524]])
print(training_set_inputs, training_set_inputs.shape)
training_set_outputs = np.array([[0.51050, 0.50950, 0.50750, 0.510, 0.5110, 0.50250, 0.50450, 0.5090, 0.50750, 0.5050, 0.503, 0.5065, 0.50750, 0.505, 0.503, 0.50650, 0.507, 0.5050, 0.49420, 0.5070, 0.499, 0.49330, 0.5095, 0.51650, 0.501, 0.51150]]).T
print(training_set_outputs, training_set_outputs.shape)
print "Random starting synaptic weights: "
print neural_network.synaptic_weights
# Train the neural network using a training set.
# Do it 10,000 times and make small adjustments each time.
neural_network.train(training_set_inputs, training_set_outputs, 10000)
print "New synaptic weights after training: "
print neural_network.synaptic_weights
user_input_one = str(input("User Input One: "))
# Test the neural network with a new situation.
print("Considering new situation [some input value] -> ?: ", user_input_one)
print(neural_network.think(np.array([user_input_one])))# enter some input value inside brackets
The neural network can be run from a terminal by the command
python myneuralnetcode.py
The screen output after running the neural network is
(array([[ 0.512 ],
[ 0.514 ],
[ 0.509 ],
[ 0.508 ],
[ 0.51 ],
[ 0.5075],
[ 0.513 ],
[ 0.5065],
[ 0.5005],
[ 0.5105],
[ 0.503 ],
[ 0.505 ],
[ 0.51 ],
[ 0.5065],
[ 0.5165],
[ 0.5175],
[ 0.522 ],
[ 0.5145],
[ 0.5135],
[ 0.5225],
[ 0.525 ],
[ 0.525 ],
[ 0.525 ],
[ 0.5245],
[ 0.508 ],
[ 0.524 ]]), (26, 1))
(array([[ 0.5105],
[ 0.5095],
[ 0.5075],
[ 0.51 ],
[ 0.511 ],
[ 0.5025],
[ 0.5045],
[ 0.509 ],
[ 0.5075],
[ 0.505 ],
[ 0.503 ],
[ 0.5065],
[ 0.5075],
[ 0.505 ],
[ 0.503 ],
[ 0.5065],
[ 0.507 ],
[ 0.505 ],
[ 0.4942],
[ 0.507 ],
[ 0.499 ],
[ 0.4933],
[ 0.5095],
[ 0.5165],
[ 0.501 ],
[ 0.5115]]), (26, 1))
Random starting synaptic weights:
[[-0.16595599]]
New synaptic weights after training:
[[ 0.04566562]]
User Input One: 0.508
('Considering new situation [some input value] -> ?: ', '0.508')
[ 0.50579927]
david@debian:~/neuralnetworks/Scikitnumpy$
Monday, 7 January 2019
A single layer perceptron with bias
A neural network will calculate lines through an origin (0,0). To calculate other lines, i.e. lines that do not pass through the origin, a bias is needed.
From the following link, I have put a neural net together
https://www.python-course.eu/neural_networks.php
Here a network classifies two clusters in a 2-dimensional space. The network will find a line that seperates the two classes. This line is called a decision boundary.
It can be run by saving the code to myfile.py and typing from a terminal in the same directory as myfile.py, python myfile.py
Here is the code
from matplotlib import pyplot as plt
import numpy as np
from collections import Counter
class Perceptron:
def __init__(self, input_length, weights=None):
if weights==None:
self.weights = np.random.random((input_length)) * 2 - 1
self.learning_rate = 0.1
@staticmethod
def unit_step_function(x):
if x < 0:
return 0
return 1
def __call__(self, in_data):
weighted_input = self.weights * in_data
weighted_sum = weighted_input.sum()
return Perceptron.unit_step_function(weighted_sum)
def adjust(self,
target_result,
calculated_result,
in_data):
error = target_result - calculated_result
for i in range(len(in_data)):
correction = error * in_data[i] *self.learning_rate
self.weights[i] += correction
def above_line(point, line_func):
x, y = point
if y > line_func(x):
return 1
else:
return 0
points = np.random.randint(1, 100, (100, 2))
class1 = [(3, 4), (4.2, 5.3), (4, 3), (6, 5), (4, 6), (3.7, 5.8),
(3.2, 4.6), (5.2, 5.9), (5, 4), (7, 4), (3, 7), (4.3, 4.3) ]
class2 = [(-3, -4), (-2, -3.5), (-1, -6), (-3, -4.3), (-4, -5.6),
(-3.2, -4.8), (-2.3, -4.3), (-2.7, -2.6), (-1.5, -3.6),
(-3.6, -5.6), (-4.5, -4.6), (-3.7, -5.8) ]
X, Y = zip(*class1)
plt.scatter(X, Y, c="r")
X, Y = zip(*class2)
plt.scatter(X, Y, c="b")
plt.show()
from itertools import chain
p = Perceptron(2)
def lin1(x):
return x + 4
for point in class1:
p.adjust(1,
p(point),
point)
for point in class2:
p.adjust(0,
p(point),
point)
evaluation = Counter()
for point in chain(class1, class2):
if p(point) == 1:
evaluation["correct"] += 1
else:
evaluation["wrong"] += 1
testpoints = [(3.9, 6.9), (-2.9, -5.9)]
for point in testpoints:
print(p(point))
print(evaluation.most_common())
from matplotlib import pyplot as plt
X, Y = zip(*class1)
plt.scatter(X, Y, c="r")
X, Y = zip(*class2)
plt.scatter(X, Y, c="b")
x = np.arange(-7, 10)
y = 5*x + 10
m = -p.weights[0] / p.weights[1]
plt.plot(x, m*x)
plt.show()
From the following link, I have put a neural net together
https://www.python-course.eu/neural_networks.php
Here a network classifies two clusters in a 2-dimensional space. The network will find a line that seperates the two classes. This line is called a decision boundary.
It can be run by saving the code to myfile.py and typing from a terminal in the same directory as myfile.py, python myfile.py
Here is the code
from matplotlib import pyplot as plt
import numpy as np
from collections import Counter
class Perceptron:
def __init__(self, input_length, weights=None):
if weights==None:
self.weights = np.random.random((input_length)) * 2 - 1
self.learning_rate = 0.1
@staticmethod
def unit_step_function(x):
if x < 0:
return 0
return 1
def __call__(self, in_data):
weighted_input = self.weights * in_data
weighted_sum = weighted_input.sum()
return Perceptron.unit_step_function(weighted_sum)
def adjust(self,
target_result,
calculated_result,
in_data):
error = target_result - calculated_result
for i in range(len(in_data)):
correction = error * in_data[i] *self.learning_rate
self.weights[i] += correction
def above_line(point, line_func):
x, y = point
if y > line_func(x):
return 1
else:
return 0
points = np.random.randint(1, 100, (100, 2))
class1 = [(3, 4), (4.2, 5.3), (4, 3), (6, 5), (4, 6), (3.7, 5.8),
(3.2, 4.6), (5.2, 5.9), (5, 4), (7, 4), (3, 7), (4.3, 4.3) ]
class2 = [(-3, -4), (-2, -3.5), (-1, -6), (-3, -4.3), (-4, -5.6),
(-3.2, -4.8), (-2.3, -4.3), (-2.7, -2.6), (-1.5, -3.6),
(-3.6, -5.6), (-4.5, -4.6), (-3.7, -5.8) ]
X, Y = zip(*class1)
plt.scatter(X, Y, c="r")
X, Y = zip(*class2)
plt.scatter(X, Y, c="b")
plt.show()
from itertools import chain
p = Perceptron(2)
def lin1(x):
return x + 4
for point in class1:
p.adjust(1,
p(point),
point)
for point in class2:
p.adjust(0,
p(point),
point)
evaluation = Counter()
for point in chain(class1, class2):
if p(point) == 1:
evaluation["correct"] += 1
else:
evaluation["wrong"] += 1
testpoints = [(3.9, 6.9), (-2.9, -5.9)]
for point in testpoints:
print(p(point))
print(evaluation.most_common())
from matplotlib import pyplot as plt
X, Y = zip(*class1)
plt.scatter(X, Y, c="r")
X, Y = zip(*class2)
plt.scatter(X, Y, c="b")
x = np.arange(-7, 10)
y = 5*x + 10
m = -p.weights[0] / p.weights[1]
plt.plot(x, m*x)
plt.show()
Saturday, 20 October 2018
Python and prediction with neural networks
The following code implements a neural network using the scripting language Python. The algorithm is from
https://www.edureka.co/blog/scikit-learn-machine-learning/
My code is Open Source without the implied warranty for suitability or fitness for any particular purpose. It can be freely distributed and copied or changed.
It is one of my first attempts at implementing a neural network and I hope I have got the shape of the x and y input vectors right. The input vector x is the opening price of shares of the Post office and the input vector y is the closing price. It gives a prediction of 0.505 which seems reasonable and within range. The prediction needs to be scaled up as all values / prices were normalised to fit between 0 and 1. An example could be, if the prices were scaled down by dividing by 100, then the true value would be 0.505 * 100 = 50.5
Python has to be installed as well as the packages python-numpy and python-sklearn.
Save the code using an editor as a file with a .py extension.
The program can by run from a terminal by
python filename.py
Here is the code.-
import numpy as np
from sklearn import svm
clf = svm.SVC(gamma=0.001, C=100)
#opening prices of shares = x
x = np.array([[0.512], [0.514], [0.509], [0.508], [0.510], [0.50750], [0.513], [0.50650], [0.50050], [0.51050], [0.503], [0.505], [0.510], [0.5065], [0.5165], [0.5175], [0.522], [0.5145], [0.5135], [0.5225], [0.525], [0.525], [0.525], [0.5245], [0.508], [0.524]])
print('Shape of x vector:')
print(x, x.shape)
#closing prices of shares = y
y = np.array([0.51050, 0.50950, 0.50750, 0.510, 0.5110, 0.50250, 0.50450, 0.5090, 0.50750, 0.5050, 0.503, 0.5065, 0.50750, 0.505, 0.503, 0.50650, 0.507, 0.5050, 0.49420, 0.5070, 0.499, 0.49330, 0.5095, 0.51650, 0.501, 0.51150]).T
print('Shape of y vector:')
print(y, y.shape)
xtrain, ytrain = x[:-1], y[:-1]
clf.fit(xtrain, ytrain) #train the data
print('Prediction for x:', clf.predict(x[-1])) #predict the data
The output on the screen is:
Shape of x vector:
(array([[ 0.512 ],
[ 0.514 ],
[ 0.509 ],
[ 0.508 ],
[ 0.51 ],
[ 0.5075],
[ 0.513 ],
[ 0.5065],
[ 0.5005],
[ 0.5105],
[ 0.503 ],
[ 0.505 ],
[ 0.51 ],
[ 0.5065],
[ 0.5165],
[ 0.5175],
[ 0.522 ],
[ 0.5145],
[ 0.5135],
[ 0.5225],
[ 0.525 ],
[ 0.525 ],
[ 0.525 ],
[ 0.5245],
[ 0.508 ],
[ 0.524 ]]), (26, 1))
Shape of y vector:
(array([ 0.5105, 0.5095, 0.5075, 0.51 , 0.511 , 0.5025, 0.5045,
0.509 , 0.5075, 0.505 , 0.503 , 0.5065, 0.5075, 0.505 ,
0.503 , 0.5065, 0.507 , 0.505 , 0.4942, 0.507 , 0.499 ,
0.4933, 0.5095, 0.5165, 0.501 , 0.5115]), (26,))
('Prediction for x:', array([ 0.505]))
his is another example of a neural network in Python. The algorithm is from
https://dev.to/shamdasani/build-a-flexible-neural-network-with-backpropagation-in-python
This neural net uses the same example of training a net with the opening and closing prices of post office shares. The code is as follows
import numpy as np
X = np.array([[0.512], [0.514], [0.509], [0.508], [0.510], [0.50750], [0.513],[0.50650], [0.50050], [0.51050], [0.503], [0.505], [0.510], [0.5065], [0.5165], [0.5175], [0.522], [0.5145], [0.5135], [0.5225], [0.525], [0.525], [0.525], [0.5245], [0.508], [0.524]])
print(X, X.shape)
y = np.array([[0.51050], [0.50950], [0.50750], [0.510], [0.5110], [0.50250], [0.50450], [0.5090], [0.50750], [0.5050], [0.503], [0.5065], [0.50750], [0.505], [0.503], [0.50650], [0.507], [0.5050], [0.49420], [0.5070], [0.499],[0.49330], [0.5095], [0.51650], [0.501], [0.5115]])
print(y, y.shape)
class Neural_Network(object):
def __init__(self):
#parameters
self.inputSize = 1
self.outputSize = 1
self.hiddenSize = 3
#weights
self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # (3x2) weight matrix from input to hidden layer
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # (3x1) weight matrix from hidden to output layer
def forward(self, X):
#forward propagation through our network
self.z = np.dot(X, self.W1) # dot product of X (input) and first set of 3x2 weights
self.z2 = self.sigmoid(self.z) # activation function
self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2) and second set of 3x1 weights
o = self.sigmoid(self.z3) # final activation function
return o
def sigmoid(self, s):
# activation function
return 1/(1+np.exp(-s))
def sigmoidPrime(self, s):
#derivative of sigmoid
return s * (1 - s)
def backward(self, X, y, o):
# backward propgate through the network
self.o_error = y - o # error in output
self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to error
self.z2_error = self.o_delta.dot(self.W2.T) # z2 error: how much our hidden layer weights contributed to output error
self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
self.W2 += self.z2.T.dot(self.o_delta) # adjusting second set (hidden --> output) weights
def train (self, X, y):
o = self.forward(X)
self.backward(X, y, o)
NN = Neural_Network()
for i in xrange(1000): # trains the NN 1,000 times
#print "Input: \n" + str(X)
#print "Actual Output: \n" + str(y)
#print "Predicted Output: \n" + str(NN.forward(X))
#print "Prediction for x: \n" + str(NN.forward(X[-1]))
#print "Loss: \n" + str(np.mean(np.square(y - NN.forward(X)))) # mean sum squared loss
#print "\n"
NN.train(X, y)
print "Prediction for x: \n" + str(NN.forward(X[-1]))
print "Loss: \n" + str(np.mean(np.square(y - NN.forward(X))))
The screen output is as follows
(array([[ 0.512 ],
[ 0.514 ],
[ 0.509 ],
[ 0.508 ],
[ 0.51 ],
[ 0.5075],
[ 0.513 ],
[ 0.5065],
[ 0.5005],
[ 0.5105],
[ 0.503 ],
[ 0.505 ],
[ 0.51 ],
[ 0.5065],
[ 0.5165],
[ 0.5175],
[ 0.522 ],
[ 0.5145],
[ 0.5135],
[ 0.5225],
[ 0.525 ],
[ 0.525 ],
[ 0.525 ],
[ 0.5245],
[ 0.508 ],
[ 0.524 ]]), (26, 1))
(array([[ 0.5105],
[ 0.5095],
[ 0.5075],
[ 0.51 ],
[ 0.511 ],
[ 0.5025],
[ 0.5045],
[ 0.509 ],
[ 0.5075],
[ 0.505 ],
[ 0.503 ],
[ 0.5065],
[ 0.5075],
[ 0.505 ],
[ 0.503 ],
[ 0.5065],
[ 0.507 ],
[ 0.505 ],
[ 0.4942],
[ 0.507 ],
[ 0.499 ],
[ 0.4933],
[ 0.5095],
[ 0.5165],
[ 0.501 ],
[ 0.5115]]), (26, 1))
Prediction for x:
[ 0.50697534]
Loss:
2.58872126395e-05
https://www.edureka.co/blog/scikit-learn-machine-learning/
My code is Open Source without the implied warranty for suitability or fitness for any particular purpose. It can be freely distributed and copied or changed.
It is one of my first attempts at implementing a neural network and I hope I have got the shape of the x and y input vectors right. The input vector x is the opening price of shares of the Post office and the input vector y is the closing price. It gives a prediction of 0.505 which seems reasonable and within range. The prediction needs to be scaled up as all values / prices were normalised to fit between 0 and 1. An example could be, if the prices were scaled down by dividing by 100, then the true value would be 0.505 * 100 = 50.5
Python has to be installed as well as the packages python-numpy and python-sklearn.
Save the code using an editor as a file with a .py extension.
The program can by run from a terminal by
python filename.py
Here is the code.-
import numpy as np
from sklearn import svm
clf = svm.SVC(gamma=0.001, C=100)
#opening prices of shares = x
x = np.array([[0.512], [0.514], [0.509], [0.508], [0.510], [0.50750], [0.513], [0.50650], [0.50050], [0.51050], [0.503], [0.505], [0.510], [0.5065], [0.5165], [0.5175], [0.522], [0.5145], [0.5135], [0.5225], [0.525], [0.525], [0.525], [0.5245], [0.508], [0.524]])
print('Shape of x vector:')
print(x, x.shape)
#closing prices of shares = y
y = np.array([0.51050, 0.50950, 0.50750, 0.510, 0.5110, 0.50250, 0.50450, 0.5090, 0.50750, 0.5050, 0.503, 0.5065, 0.50750, 0.505, 0.503, 0.50650, 0.507, 0.5050, 0.49420, 0.5070, 0.499, 0.49330, 0.5095, 0.51650, 0.501, 0.51150]).T
print('Shape of y vector:')
print(y, y.shape)
xtrain, ytrain = x[:-1], y[:-1]
clf.fit(xtrain, ytrain) #train the data
print('Prediction for x:', clf.predict(x[-1])) #predict the data
The output on the screen is:
Shape of x vector:
(array([[ 0.512 ],
[ 0.514 ],
[ 0.509 ],
[ 0.508 ],
[ 0.51 ],
[ 0.5075],
[ 0.513 ],
[ 0.5065],
[ 0.5005],
[ 0.5105],
[ 0.503 ],
[ 0.505 ],
[ 0.51 ],
[ 0.5065],
[ 0.5165],
[ 0.5175],
[ 0.522 ],
[ 0.5145],
[ 0.5135],
[ 0.5225],
[ 0.525 ],
[ 0.525 ],
[ 0.525 ],
[ 0.5245],
[ 0.508 ],
[ 0.524 ]]), (26, 1))
Shape of y vector:
(array([ 0.5105, 0.5095, 0.5075, 0.51 , 0.511 , 0.5025, 0.5045,
0.509 , 0.5075, 0.505 , 0.503 , 0.5065, 0.5075, 0.505 ,
0.503 , 0.5065, 0.507 , 0.505 , 0.4942, 0.507 , 0.499 ,
0.4933, 0.5095, 0.5165, 0.501 , 0.5115]), (26,))
('Prediction for x:', array([ 0.505]))
his is another example of a neural network in Python. The algorithm is from
https://dev.to/shamdasani/build-a-flexible-neural-network-with-backpropagation-in-python
This neural net uses the same example of training a net with the opening and closing prices of post office shares. The code is as follows
import numpy as np
X = np.array([[0.512], [0.514], [0.509], [0.508], [0.510], [0.50750], [0.513],[0.50650], [0.50050], [0.51050], [0.503], [0.505], [0.510], [0.5065], [0.5165], [0.5175], [0.522], [0.5145], [0.5135], [0.5225], [0.525], [0.525], [0.525], [0.5245], [0.508], [0.524]])
print(X, X.shape)
y = np.array([[0.51050], [0.50950], [0.50750], [0.510], [0.5110], [0.50250], [0.50450], [0.5090], [0.50750], [0.5050], [0.503], [0.5065], [0.50750], [0.505], [0.503], [0.50650], [0.507], [0.5050], [0.49420], [0.5070], [0.499],[0.49330], [0.5095], [0.51650], [0.501], [0.5115]])
print(y, y.shape)
class Neural_Network(object):
def __init__(self):
#parameters
self.inputSize = 1
self.outputSize = 1
self.hiddenSize = 3
#weights
self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # (3x2) weight matrix from input to hidden layer
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # (3x1) weight matrix from hidden to output layer
def forward(self, X):
#forward propagation through our network
self.z = np.dot(X, self.W1) # dot product of X (input) and first set of 3x2 weights
self.z2 = self.sigmoid(self.z) # activation function
self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2) and second set of 3x1 weights
o = self.sigmoid(self.z3) # final activation function
return o
def sigmoid(self, s):
# activation function
return 1/(1+np.exp(-s))
def sigmoidPrime(self, s):
#derivative of sigmoid
return s * (1 - s)
def backward(self, X, y, o):
# backward propgate through the network
self.o_error = y - o # error in output
self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to error
self.z2_error = self.o_delta.dot(self.W2.T) # z2 error: how much our hidden layer weights contributed to output error
self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
self.W2 += self.z2.T.dot(self.o_delta) # adjusting second set (hidden --> output) weights
def train (self, X, y):
o = self.forward(X)
self.backward(X, y, o)
NN = Neural_Network()
for i in xrange(1000): # trains the NN 1,000 times
#print "Input: \n" + str(X)
#print "Actual Output: \n" + str(y)
#print "Predicted Output: \n" + str(NN.forward(X))
#print "Prediction for x: \n" + str(NN.forward(X[-1]))
#print "Loss: \n" + str(np.mean(np.square(y - NN.forward(X)))) # mean sum squared loss
#print "\n"
NN.train(X, y)
print "Prediction for x: \n" + str(NN.forward(X[-1]))
print "Loss: \n" + str(np.mean(np.square(y - NN.forward(X))))
The screen output is as follows
(array([[ 0.512 ],
[ 0.514 ],
[ 0.509 ],
[ 0.508 ],
[ 0.51 ],
[ 0.5075],
[ 0.513 ],
[ 0.5065],
[ 0.5005],
[ 0.5105],
[ 0.503 ],
[ 0.505 ],
[ 0.51 ],
[ 0.5065],
[ 0.5165],
[ 0.5175],
[ 0.522 ],
[ 0.5145],
[ 0.5135],
[ 0.5225],
[ 0.525 ],
[ 0.525 ],
[ 0.525 ],
[ 0.5245],
[ 0.508 ],
[ 0.524 ]]), (26, 1))
(array([[ 0.5105],
[ 0.5095],
[ 0.5075],
[ 0.51 ],
[ 0.511 ],
[ 0.5025],
[ 0.5045],
[ 0.509 ],
[ 0.5075],
[ 0.505 ],
[ 0.503 ],
[ 0.5065],
[ 0.5075],
[ 0.505 ],
[ 0.503 ],
[ 0.5065],
[ 0.507 ],
[ 0.505 ],
[ 0.4942],
[ 0.507 ],
[ 0.499 ],
[ 0.4933],
[ 0.5095],
[ 0.5165],
[ 0.501 ],
[ 0.5115]]), (26, 1))
Prediction for x:
[ 0.50697534]
Loss:
2.58872126395e-05
Subscribe to:
Posts (Atom)

