⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perception.rb

📁 Perception,神经网络感知器
💻 RB
字号:
class Perception  def initialize(size)    @weight = Array.new(size+1, 1)    @size = size    @count = 0    @eta = 0.01  end    def train(input, output)    o = 0    0.upto(@size - 1) do |i|     o += (input[i] * @weight[i])     end    o += @weight[@size]    0.upto(@size-1) do |i|      delta = @eta / @count * (output - o) * input[i]      @weight[i] += delta    end    delta = @eta / @count * (output - o)    @weight[@size] += delta  end    def train(input, output, filename, time)    @count += 1        o = 0    0.upto(@size - 1) do |i|     o += (input[i] * @weight[i])     end    o += @weight[@size]    0.upto(@size-1) do |i|      delta = @eta / @count * (output - o) * input[i]      @weight[i] += delta    end        delta = @eta / @count * (output - o)    @weight[@size] += delta        e = 0        File.open(filename, "r") do |file|      0.upto(time-1) do        line = file.gets        result = line.split        input = Array.new(2)        input[0] = result[0].to_f        input[1] = result[1].to_f        output = result[2].to_f                o = 0        0.upto(@size - 1) do |i|         o += (input[i] * @weight[i])         end        o += @weight[@size]                e += (output - o) * (output - o)              end    end    puts e / 2  end    def test(input)    output = 0    0.upto(size-1) do |i|      output += input[i] * @weight[i]    end    output += @weight[size]    output  end    def output    0.upto(@size) do |i|      puts @weight[i]    end  endendtraining_data = ARGV[0]time = ARGV[1].to_i# test_data = ARGV[1]perception = Perception.new(2)0.upto 10 doFile.open(training_data, "r") do |file|  0.upto(time - 1) do    line = file.gets    result = line.split    input = Array.new(2)    input[0] = result[0].to_f    input[1] = result[1].to_f    output = result[2].to_f    perception.train(input, output, training_data, time)  endendendperception.output# File.open(test_data, "r") do |file|#   while line = file.gets#     result = line.split#     intput = Array.new(2)#     input[0] = result[0].to_f#     input[1] = result[1].to_f#     puts perception.test(input)#   end# end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -