class TNN::HopFieldNetwork

Attributes

dim[RW]
is_train[RW]
net[RW]
nodes[RW]
threshold[RW]
train_datas[RW]

Public Class Methods

new(threshold=nil, data) click to toggle source
# File lib/t_nn/hop_field_network.rb, line 9
def initialize(threshold=nil, data)
  if threshold == nil
    @threshold = 0.0
  else
    @threshold = threshold
  end
  @train_datas=Array.new 
  load_train_data(data)
  @nodes = Array.new(@train_datas[0].length, 1.0)
  @dim = @train_datas[0].length
  @net = Array.new(@dim**2,0.0) 

end

Public Instance Methods

calc_connected_factor(target_node_id) click to toggle source
# File lib/t_nn/hop_field_network.rb, line 56
def calc_connected_factor(target_node_id)
  sum = 0.0
  @nodes.each_with_index do |node,node_id|
    sum += @net[target_node_id*@dim + node_id] * node if (target_node_id != node_id )
  end
  return sum
end
energy() click to toggle source

calc energy function

# File lib/t_nn/hop_field_network.rb, line 75
def energy
  sum = 0.0
  @nodes.each_with_index do |node,node_id|
    @nodes.each_with_index do |node2,node2_id|
      sum += @net[node2_id*@dim + node_id] * node * node2 if ( node != node2)
    end
  end
  sum2 = 0.0 
  @nodes.each do |node|
    sum2 += @threshold * node 
  end

  result = (-1.0/2.0)*sum + sum2
  return result
end
load_train_data(data) click to toggle source
# File lib/t_nn/hop_field_network.rb, line 92
def load_train_data(data)
  @train_datas.push(data)
end
memorize() click to toggle source
# File lib/t_nn/hop_field_network.rb, line 23
def memorize
  @nodes.length.times do |node_id|
    @nodes.length.times do |node2_id|
      sum = 0.0 
      @train_datas.each do |train_data|
        sum += train_data[node_id] * train_data[node2_id] if(node_id != node2_id)
      end
      @net[node_id * @dim + node2_id] = sum
      @net[node2_id*@dim + node_id] = sum
    end
  end
end
remember(datas) click to toggle source

@param [Array] datas datas which has noise

# File lib/t_nn/hop_field_network.rb, line 41
def remember(datas)
  @nodes = datas
  e = energy
  loop do
    @nodes.each_with_index do |node,node_id|
      internal_w = calc_connected_factor(node_id) 
      update_external_w(node_id,internal_w)
    end
    new_e = energy
    break if (e == new_e)
    e = new_e
  end
  puts "energy : #{energy}"
end
update_external_w(node_id,i_w) click to toggle source
# File lib/t_nn/hop_field_network.rb, line 64
def update_external_w(node_id,i_w)
  if i_w >= @threshold
    @nodes[node_id] = 1.0
  else
    @nodes[node_id] = -1.0
  end
end