Created
July 11, 2016 22:01
-
-
Save JoshCheek/79183960c45c90c6f4c877317e98a2b6 to your computer and use it in GitHub Desktop.
An expanded version of the prime memorizing (overfitting) neural network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def neuron_value(input_neuron_values, synapses) | |
# The additional neuron with a value of 1 allows it to output 1 (on), even when all the inputs are 0 (off) | |
neurons_with_synapses = (input_neuron_values+[1]).zip(synapses) | |
total_input = neurons_with_synapses.reduce(0) { |sum, (neuron, synapse)| sum + neuron*synapse } | |
# This is the logistic function https://en.wikipedia.org/wiki/Logistic_function | |
# with L=1, k=1, x=input, x_0=0 | |
1 / (1 + Math.exp(-total_input)) | |
end | |
def run_nn(input_layer, all_synapses) | |
all_synapses.reduce(input_layer) do |prev_neurons, layer_synapses| | |
layer_synapses.map { |synapses| neuron_value(prev_neurons, synapses) } | |
end | |
end | |
def nn_prime?(n) | |
all_synapses = [ | |
[ # input layer to hidden layer | |
[-9, 3, 9, 5, -5, 6, -6, 6, -6], | |
[-2, 2, -3, -6, 0, 6, -5, -6, -3], | |
[-4, -4, 7, 0, -3, -3, 2, -2, 1], | |
[ 2, -9, -3, 8, -4, -9, -3, 6, 6], | |
[-2, 4, 11, 3, -6, 1, -7, 2, -3], | |
[ 1, -7, 3, 4, -4, -2, 3, 0, -3.5], | |
[-9, 6, 5, -4, 6, 5, 5, -4, -6], | |
[-4, -7, -4, 5, 2, -7, 8, -4, 0], | |
[ 1, -3, -7, -3, 2, -5, 6, -5, -2] | |
], | |
[ # hidden layer to output layer | |
[-15,-14,-9,-11,14,11,-11,-11,11,5] | |
], | |
] | |
input_bits = 8.times.map { |i| n[i] } | |
run_nn(input_bits, all_synapses)[0] > 0.5 # only one output neuron, prime when it's value is in the upper half | |
end | |
require 'prime' | |
def nn_is_correct?(n) | |
nn_prime?(n) == Prime.prime?(n) | |
end | |
256.times.all? { |n| nn_is_correct?(n) } # => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment