|
| 1 | +#!/usr/local/bin/python3 |
| 2 | +# -*-coding:utf-8 -*- |
| 3 | +""" |
| 4 | +# ================================================== |
| 5 | +# Name : neuron |
| 6 | +# Action : module for neuron |
| 7 | +# Explanation : make a module for neuron |
| 8 | +# Self Class used : neuron |
| 9 | +# Note : |
| 10 | +# ================================================== |
| 11 | +""" |
| 12 | + |
| 13 | +from module_for_all import log_thing |
| 14 | +import numpy as np |
| 15 | +import random |
| 16 | + |
| 17 | +from module_for_all.log_thing import Basic |
| 18 | + |
| 19 | + |
| 20 | +class neuron_original(log_thing.Timer): |
| 21 | + """ |
| 22 | +* ================================================== |
| 23 | +* Self Class : neuron |
| 24 | +* Extends : log_thing.Timer |
| 25 | +* Explanation : 基本單一 neuron 設置 |
| 26 | +* ================================================== |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, inputX: np.array, outputY: float, weight: np.array, bias: float): |
| 30 | + """ |
| 31 | + 1. need to super, send a class object |
| 32 | + 2. make neuron's basic setting |
| 33 | + """ |
| 34 | + super().__init__(self.__class__) |
| 35 | + |
| 36 | + self.inputX = inputX |
| 37 | + self.outputY = outputY |
| 38 | + self.weight = weight |
| 39 | + self.bias = bias |
| 40 | + return |
| 41 | + |
| 42 | + def nextInput(self): |
| 43 | + print("### 1") |
| 44 | + return |
| 45 | + |
| 46 | + def nextWeight(self): |
| 47 | + return |
| 48 | + |
| 49 | + def nextBias(self): |
| 50 | + return |
| 51 | + |
| 52 | + |
| 53 | +class neuron(neuron_original): |
| 54 | + """ |
| 55 | +* ================================================== |
| 56 | +* Self Class : neuron |
| 57 | +* Extends : neuron_original |
| 58 | +* Explanation : 包裝 neuron. 多上了數值檢測, 分配等等 |
| 59 | +* ================================================== |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__(self, inputArray: list, outputCorrect: float, weight: list = None, bias: float = None, isList=False): |
| 63 | + """ |
| 64 | + 1. need to super, to make neuron |
| 65 | + 2. used to check data |
| 66 | + """ |
| 67 | + |
| 68 | + self.ors_inArray = inputArray |
| 69 | + self.ors_outCorrect = outputCorrect |
| 70 | + |
| 71 | + self.inArray = None |
| 72 | + self.outCorrect = None |
| 73 | + |
| 74 | + self.isList = isList |
| 75 | + |
| 76 | + if weight is None: |
| 77 | + self.weight = random.uniform(0, 1) |
| 78 | + print(self.weight) |
| 79 | + else: |
| 80 | + self.weight = weight |
| 81 | + |
| 82 | + if bias is None: |
| 83 | + self.bias = random.uniform(0, 1) |
| 84 | + print(self.bias) |
| 85 | + else: |
| 86 | + self.bias = bias |
| 87 | + |
| 88 | + self.checkAllVar() |
| 89 | + |
| 90 | + super().__init__(self.inArray, self.outCorrect, self.weight, self.bias) |
| 91 | + return |
| 92 | + |
| 93 | + def checkAllVar(self): |
| 94 | + |
| 95 | + self.inArray = self.checkArray("ors_inArray", self.ors_inArray) |
| 96 | + self.outCorrect = self.checkFloat("ors_outCorrect", self.ors_outCorrect) |
| 97 | + |
| 98 | + return |
| 99 | + |
| 100 | + def checkArray(self, varName, tmpVar): |
| 101 | + |
| 102 | + if type(tmpVar) == list: |
| 103 | + tmpVar = np.array(tmpVar) |
| 104 | + |
| 105 | + if not self.isList: |
| 106 | + print("need to check, is type of \"" + varName + "\" list?") |
| 107 | + |
| 108 | + elif type(tmpVar) == np.array: |
| 109 | + return tmpVar |
| 110 | + |
| 111 | + else: |
| 112 | + print("need to check, is type of \"" + varName + "\" list or np.array!") |
| 113 | + return None |
| 114 | + |
| 115 | + return tmpVar |
| 116 | + |
| 117 | + def checkFloat(self, varName, tmpVar): |
| 118 | + |
| 119 | + if type(tmpVar) == int: |
| 120 | + tmpVar = float(tmpVar) |
| 121 | + |
| 122 | + elif type(tmpVar) == complex: |
| 123 | + print("need to check, type of \"" + varName + "\" need to int or float!") |
| 124 | + return None |
| 125 | + |
| 126 | + elif type(tmpVar) == float: |
| 127 | + return tmpVar |
| 128 | + |
| 129 | + else: |
| 130 | + print("need to check, type of \"" + varName + "\" need to int or float!") |
| 131 | + return None |
| 132 | + |
| 133 | + return tmpVar |
| 134 | + |
| 135 | + def nextInput(self): |
| 136 | + super().nextInput() |
| 137 | + print("### 2") |
| 138 | + return |
| 139 | + |
| 140 | + |
| 141 | +""" |
| 142 | +x_all = sum(i=1 to n, weight_i * x_i) - bias |
| 143 | +y_sigmoid = 1/(1+exp(-x_all)) or 1/(1+5exp(-5*x_all)) |
| 144 | +
|
| 145 | +diffDelta(p) = y_correct(p) - y_now(p) |
| 146 | +
|
| 147 | +weight(p+1) = weight(p) + ΔWeight(p) |
| 148 | +ΔWeight(p) = α * y_now(p) * δ(p) |
| 149 | +
|
| 150 | +α : 自訂的學習率 |
| 151 | +δ : 誤差梯度 |
| 152 | +
|
| 153 | +δ(p) = ∂(y_now(p))/∂(x_all(p)) * diffDelta(p) |
| 154 | + = y_now(p) * (1-y_now(p)) * diffDelta(p) |
| 155 | +""" |
| 156 | + |
| 157 | +inX1 = [0, 0, 1, 1, 0, 1, 0, 1] |
| 158 | +inX2 = [0, 1, 0, 1, 1, 1, 0, 0] |
| 159 | +outCorrect = [0, 0, 0, 1, 0, 1, 0, 0] |
| 160 | + |
| 161 | +tmpNeuron = neuron([inX1[0], inX2[0]], outCorrect[0]) |
| 162 | +tmpNeuron.nextInput() |
| 163 | +tmpNeuron.timerStart() |
| 164 | +tmpNeuron.timerNow() |
| 165 | +tmpNeuron.timerEnd() |
0 commit comments