Class: Neuron
- Inherits:
-
Object
- Object
- Neuron
- Defined in:
- lib/nn.rb
Instance Attribute Summary collapse
-
#bias ⇒ Object
readonly
Returns the value of attribute bias.
-
#weights ⇒ Object
readonly
Returns the value of attribute weights.
Instance Method Summary collapse
- #calc(inputs) ⇒ Object
-
#initialize(number_of_inputs, activation_function) ⇒ Neuron
constructor
A new instance of Neuron.
- #parameters ⇒ Object
- #reset_params ⇒ Object
- #set_params(params) ⇒ Object
Constructor Details
#initialize(number_of_inputs, activation_function) ⇒ Neuron
Returns a new instance of Neuron.
5 6 7 8 9 10 11 12 13 |
# File 'lib/nn.rb', line 5 def initialize(number_of_inputs, activation_function) @initial_weights = Array.new(number_of_inputs) { rand(-1.0..1.0) } @initial_bias = rand(-1.0..1.0) @weights = @initial_weights.map { |w| Value.new(w) } @bias = Value.new(@initial_bias) @activation_function = activation_function end |
Instance Attribute Details
#bias ⇒ Object (readonly)
Returns the value of attribute bias.
29 30 31 |
# File 'lib/nn.rb', line 29 def bias @bias end |
#weights ⇒ Object (readonly)
Returns the value of attribute weights.
29 30 31 |
# File 'lib/nn.rb', line 29 def weights @weights end |
Instance Method Details
#calc(inputs) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/nn.rb', line 35 def calc(inputs) # xw + b n = self.weights.size raise "Wrong number of inputs! #{inputs.size} expected #{n}" unless n == inputs.size sum = self.bias n.times do |index| sum += self.weights[index] * inputs[index] end if @activation_function == :tanh sum.tanh elsif @activation_function == :relu sum.relu elsif @activation_function == :sigmoid sum.sigmoid else raise "Unsupported activation function: #{activation_function}" end end |
#parameters ⇒ Object
31 32 33 |
# File 'lib/nn.rb', line 31 def parameters self.weights + [self.bias] end |
#reset_params ⇒ Object
15 16 17 18 19 20 |
# File 'lib/nn.rb', line 15 def reset_params @initial_weights.each_with_index do |w,i| @weights[i].value = w end @bias.value = @initial_bias end |
#set_params(params) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/nn.rb', line 22 def set_params(params) n = 1 + @weights.size raise "Illegal number of parameters: #{params.size} expected #{n}" if n != params.size @bias.value = params[0] (1...params.size).each { |i| @weights[i - 1].value = params[i] } end |