Given the latitude and longitude, create a program that determines whether it belongs to Tokyo or Kanagawa.
Judgment is made by a neural network with two input layers, two intermediate layers, and one output layer.
class Neuron:
input_sum = 0.0
output = 0.0
def setInput(self, inp):
self.input_sum += inp
def getOutput(self):
self.output = sigmoid(self.input_sum)
return self.output
def reset(self):
self.input_sum = 0
self.output = 0
setInput
Sum the inputs.
getOutput
Outputs the value obtained by converting the input value with the activation function.
Neurons have the characteristic of firing when the input value exceeds the threshold value, which is mimicked by applying the sigmoid function to the activation function as described above.
reset
Reset the input value.
class NeuralNetwork:
#Input weight
w_im = [[0.496, 0.512], [-0.501, 0.990], [0.490, -0.502]] # [[i1-m1, i1-m2], [i2-m1, i2-m2], [bias1-m1, bias1-m2]]
w_mo = [0.121, -0.4996, 0.200] # [m1-o, m2-o, bias2-0]
#Declaration of each layer
input_layer = [0.0, 0.0, 1.0]
middle_layer = [Neuron(), Neuron(), 1.0]
ouput_layer = Neuron()
...
ʻThe third element of input_layer and
middle_layer` is bias.
Read a file that describes longitude and latitude on one line.
35.48,137.76
35.47,137.81
35.29,138.06
...
It is possible to distinguish between Tokyo and Kanagawa for complicated boundaries. To make a proper judgment, it is necessary to set the weight and threshold of each input appropriately. The setting method will be learned from the next time onwards.
Recommended Posts