LoginSignup
0
0

More than 3 years have passed since last update.

Python&機械学習 勉強メモ③:ニューラルネットワーク

Last updated at Posted at 2020-04-01



の続き

参考教材

Udemy みんなのAI講座 ゼロからPythonで学ぶ人工知能と機械学習

課題設定

緯度・経度を与えると、東京・神奈川のどちらに属するかを判定するプログラムを作成する.

判定方法

入力層2つ・中間層2つ・出力層1つのニューラルネットワークで判定する。

ニューロンクラス

class Neuron:
    input_sum = 0.0
    output = 0.0

    def setInput(self, inp):
        self.input_sum += inp

    def getOutput(self):
        self.output = sigmoid(self.input_sum)
        return self.output

    def reset(self):
        self.input_sum = 0
        self.output = 0

setInput

入力を合計する。

getOutput

入力値を活性化関数で変換した値を出力する。
ニューロンは入力値が閾値を超えると発火する特徴があるが、上記のように活性化関数にシグモイド関数を適用することでそれを模倣する。

reset

入力値をリセットする。

ニューラルネットワーク

class NeuralNetwork:
    # 入力の重み
    w_im = [[0.496, 0.512], [-0.501, 0.990], [0.490, -0.502]] # [[i1-m1, i1-m2], [i2-m1, i2-m2], [bias1-m1, bias1-m2]]
    w_mo = [0.121, -0.4996, 0.200] # [m1-o, m2-o, bias2-0]
    # 各層の宣言
    input_layer = [0.0, 0.0, 1.0]
    middle_layer = [Neuron(), Neuron(), 1.0]
    ouput_layer = Neuron()
    ...

input_layermiddle_layerの3番目の要素はバイアス。

入力形式とファイル読み込み

入力ファイル

1行に 経度,緯度 を記載しているファイルを読み込む。

35.48,137.76
35.47,137.81
35.29,138.06
...

得られる結果

Figure_1.png

複雑な境界線に対して東京・神奈川を区別できている。
きちんと判定するには、各入力の重みづけや閾値を適切に設定する必要があるはず。
その設定方法は次回以降に学習。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0