Skip to content

Commit a7ef2b0

Browse files
committed
2017/10/14
1 parent 79b3f55 commit a7ef2b0

File tree

8 files changed

+227
-4
lines changed

8 files changed

+227
-4
lines changed

develop/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from develop import dev_ANNs

develop/dev_ANNs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from develop.dev_ANNs.neuron.neuron import neuron

develop/dev_ANNs/neuron/__init__.py

Whitespace-only changes.

develop/dev_ANNs/neuron/neuron.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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()

import_main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/local/bin/python3
2+
# -*-coding:utf-8 -*-
3+
import sys
4+
import os

learning/main_learning.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
# ==================================================
1010
"""
1111

12-
from module_for_all import log_thing
13-
from learning.module import tf_class
12+
13+
1414
import sys
1515
import os
1616
from functools import wraps
17+
print("###")
18+
print(sys.path)
19+
print("###")
20+
print(__file__)
21+
print("###")
1722

23+
from module_for_all import log_thing
24+
from learning.module import tf_class
1825

1926
class main_learning(log_thing.Timer):
2027
"""

main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
"""
1212

1313
from module_for_all import log_thing
14-
import sys
15-
import os
14+
# import sys
15+
# import os
16+
from import_main import *
1617

1718
__author__ = "Asa Ewing"
1819
__version__ = "1.0.0"
1920

21+
print(sys.path)
22+
2023

2124
class mainPiBot(log_thing.Timer):
2225
"""

module_for_all/log_thing.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,48 @@
1616
import time
1717
import subprocess
1818

19+
import inspect
20+
import re
21+
22+
23+
class Basic(object):
24+
def __init__(self):
25+
return
26+
27+
@staticmethod
28+
def VarName(var):
29+
30+
for tmp in inspect.getframeinfo(inspect.currentframe().f_back):
31+
print("# # # ", tmp)
32+
33+
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
34+
m = re.search(r"\bVarName\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)", line)
35+
if m:
36+
return m.group(1)
37+
38+
@staticmethod
39+
def VarName2(*args):
40+
frame = inspect.currentframe()
41+
frame = inspect.getouterframes(frame)[1]
42+
string = inspect.getframeinfo(frame[0]).code_context[0].strip()
43+
args = string[string.find('(') + 1:-1].split(',')
44+
45+
names = []
46+
for i in args:
47+
if i.find('=') != -1:
48+
names.append(i.split('=')[1].strip())
49+
#names = i.split('=')[1].strip()
50+
else:
51+
names.append(i)
52+
#names = i
53+
54+
return names
55+
56+
@staticmethod
57+
def foo(**kwargs):
58+
for arg_name in kwargs:
59+
return kwargs[arg_name], arg_name
60+
1961

2062
class DateTime(object):
2163
"""

0 commit comments

Comments
 (0)