[PYTHON] Learning from the basics Artificial intelligence textbook Chapter 5 Chapter end problems

Introduction

As a study of machine learning, I am reading "* Learning from the basics: Artificial intelligence textbook *".

The feature of this book is that the end-of-chapter problem contains a simple program of Python.

Here, it is copied with Ruby.

Chapter 5 End of Chapter Problem

neuralnet.rb


INPUTNO = 2
HIDDENNO = 2

def forward(wh, wo, hi, e)
  HIDDENNO.times do |i|
    u = 0.0
    INPUTNO.times do |j|
      u += e[j] * wh[i][j]
    end
    u -= wh[i][INPUTNO]
    hi[i] = f(u)
  end
  o = 0.0
  HIDDENNO.times do |i|
    o += hi[i] * wo[i]
  end
  o -= wo[HIDDENNO]
  f(o)
end

def f(u)
  return 1.0 if u >= 0
  0.0
end

wh = [[-2, 3, -1], [-2, 1, 0.5]]
wo = [-60, 94, -1]
e = [[0, 0], [0, 1], [1, 0], [1, 1]]
hi = [0] * (HIDDENNO + 1)

e.each do |i|
  puts "#{i}->#{forward(wh, wo, hi, i)}"
end

It is a simple hierarchical neural network calculation and there is no learning, but please be careful because there is an error in the indentation on the 26th line ** (September 25, 2019, 1st edition, 1st print issued) **

error.py


for i in range(HIDDENNO):     #Wrong

    for i in range(HIDDENNO): #Positive

As you know, indentation errors in Python seem fatal, but fortunately you can download the sample code from the Ohmsha book page.

step.rb


def f(u)
  return 1.0 if u >= 0
  0.0
end

 #Output example
[0, 0]->0.0
[0, 1]->1.0
[1, 0]->1.0
[1, 1]->0.0

The transfer function f is a step function

Sigmoid.rb


def f(u)
  1 / (1 + Math::E ** -u)
end

 #Output example
[0, 0]->0.0006265270712940932
[0, 1]->0.6434453861326787
[1, 0]->0.0003334059232134918
[1, 1]->8.512503196901111e-16

The transfer function f is a sigmoid function

ramp.rb


def f(u)
  return u if u >= 0
  0.0
end

 #Output example
[0, 0]->0.0
[0, 1]->1.0
[1, 0]->0.0
[1, 1]->0.0

The transfer function f is the ramp function

Summary

Recommended Posts

Learning from the basics Artificial intelligence textbook Chapter 5 Chapter end problems
Deep Learning from the mathematical basics Part 2 (during attendance)
Othello ~ From the tic-tac-toe of "Implementation Deep Learning" (4) [End]
Pip the machine learning library from one end (Ubuntu)
Creating artificial intelligence by machine learning using TensorFlow from zero knowledge-Introduction 1
Deep Learning / Deep Learning from Zero 2 Chapter 4 Memo
Deep Learning / Deep Learning from Zero Chapter 3 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 5 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 7 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 8 Memo
Deep Learning / Deep Learning from Zero Chapter 5 Memo
Deep Learning / Deep Learning from Zero Chapter 4 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 3 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 6 Memo
Python learning memo for machine learning by Chainer until the end of Chapter 2
[Reading memo] Linux standard textbook (Chapter 7-8)
Linux standard textbook
Linux standard textbook (ver3.0.2) I tried Chapter 1
[Reading memo] Linux standard textbook (Chapter 1 to Chapter 6)
Linux standard textbook memo 1
Linux standard textbook memo 3
Linux standard textbook part 5
Linux standard textbook part 4
Linux standard textbook memo 1 part 2
I read "Linux standard textbook"!
Linux standard textbook memo part 6
[Chapter 8] Econometrics (Yuhikaku) Chapter End Problem, Answer by python
[Chapter 6] Econometrics (Yuhikaku) Chapter End Problem (Demonstration), Answer by python
Learning from the basics Artificial intelligence textbook Chapter 5 Chapter end problems
[Learning memo] Deep Learning made from scratch [Chapter 7]
Learning notes from the beginning of Python 1
Deep learning / Deep learning made from scratch Chapter 6 Memo
[Learning memo] Deep Learning made from scratch [Chapter 5]
[Learning memo] Deep Learning made from scratch [Chapter 6]
Deep learning / Deep learning made from scratch Chapter 7 Memo
Learning notes from the beginning of Python 2
Deep Learning from mathematical basics (during attendance)
Mathematical statistics from the basics Random variables
[Learning memo] Deep Learning made from scratch [~ Chapter 4]
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3