インターネットデパート - 取扱い商品数1000万点以上の通販サイト。送料無料商品も多数あります。

Pythonで実装、はじめての機械学習: 単純パーセプトロン、ニューラルネットワーク、ディープラーニング

価格: ¥0
カテゴリ: Kindle版
Amazon.co.jpで確認
Rで実装、はじめての機械学習3部作を合併して、RからPythonに書き換えた
3部作:B071KPSS69、B073WGFG3S、B074JF8J7H

以下のコードはインデントに全角空白を利用しているので、
コピペしたあと置換する事

-myarray.py
# -*- coding: utf-8 -*-

class MyArray :

 def __init__ ( self , array ) :

  self.array = array

 def add ( self , value ) :
  self.array.append ( value )

 def set_array ( self , value ) :
  self.array = value

 def set ( self , i , value ) :
  self.array [ i ] = value

 def len ( self ) :
  return len ( self.array )

 def v ( self , i ) :
  return self.array [ i ]

 def sum ( self ) :
  return sum ( self.array )

 def __add__ ( self , arg ) :
  return self.calc ( arg , lambda x , y : x + y )

 def __sub__ ( self , arg ) :
  return self.calc ( arg , lambda x , y : x - y )

 def __rsub__ ( self , value ) :
  res = MyArray ([])
  for i in range ( self.len () ) :
   res.add ( value - self.array[ i ] )
  return res

 def __mul__ ( self , arg ) :
  return self.calc ( arg , lambda x , y : x * y )

 def __rmul__ ( self , value ) :
  res = MyArray ([])
  for i in range ( self.len () ) :
   res.add ( self.array[ i ] * value )
  return res

 def calc ( self , arg , opt ) :
  res = MyArray ([])
  for i in range ( self.len () ) :
   res.add ( opt ( self.array[ i ] , arg.v ( i ) ) )
  return res

 def __str__( self ) :
  return 'MyArray:' + str ( self.array )



-unit.py(ディープラーニングで利用)

# -*- coding: utf-8 -*-

import copy
import util as util
import common as com
import myfile as file
from myarray import MyArray

class Unit :

 def __init__ ( self ) :

  self.m_outs = []

  self.out = MyArray([])

 def initialize ( self ,
   len_unit , len_w ,
   len_layer ,
   len_m_unit , len_m_w ) :

  self.m_layer = com.init_m_layer (
       len_layer , len_m_unit , len_m_w )

  self.ws = com.r_matrix ( len_unit , len_w )

 def process_output ( self , inputs , in_row ) :

  self.m_outs = []

  a = inputs [ in_row ]

  for m_ws in self.m_layer :

   a = com.output ( m_ws , a )

   a.add ( 1 )

   self.m_outs.append ( a )

  self.out = com.output ( self.ws , a )

 def process_update_weight (
   self , rate , correct , inputs , in_row ) :

  m_layer_update = copy.deepcopy ( self.m_layer )

  out_delta =
   -1 * ( correct [ in_row ] - self.out )
    * self.out * ( 1 - self.out )

  delta_L1 = out_delta

  w_L1 = self.ws

  for layer in reversed (
     range ( len ( self.m_layer ) ) ) :

   m_ws = copy.deepcopy ( self.m_layer [ layer ] )

   delta_L = MyArray ( [] )
   delta_L.set_array ( [0] * len ( m_ws ) )

   for i in range ( len ( m_ws ) ) :

    m_out_tmp = self.m_outs [ layer ]

    tmp1 = 0

    for k in range ( delta_L1.len() ) :
     tmp1 = tmp1 +
      delta_L1.v ( k ) * w_L1[ k ].v ( i ) *
      m_out_tmp.v ( i ) * ( 1 - m_out_tmp.v ( i ) )

    delta_L.set ( i , tmp1 )

    for j in range ( m_ws[0].len() ) :

     m_out_L_1 = 0

     if layer == 0 :
      m_out_L_1 = inputs [ in_row ].v ( j )
     else :
      m_out_L_1 = self.m_outs [ layer - 1 ].v ( j )

     tmp2 = m_ws [ i ].v ( j ) -
       rate * delta_L.v ( i ) * m_out_L_1

     m_layer_update [ layer ] [ i ] .set ( j , tmp2 )
     
   delta_L1 = delta_L

   w_L1 = m_ws

  self.m_layer = m_layer_update

  for i in range ( len ( self.ws ) ) :
   for j in range ( self.ws [ i ] .len () ) :

    tmp3 = self.ws [ i ].v ( j ) - rate *
    out_delta.v ( i ) * self.m_outs [ -1 ].v ( j )

    self.ws [ i ].set ( j , tmp3 )



-util.py
# -*- coding: utf-8 -*-

import random

def r ( ) :
 return random.uniform ( -1 , 1 )

def diff ( correct , out ) :
 tmp = correct - out

 res = tmp * tmp
 
 return res.sum ()



以上