From 58a1092af6a7f9b2a5c7aa7db6cbf0fc0f098140 Mon Sep 17 00:00:00 2001 From: YLI Date: Fri, 1 Mar 2019 13:17:45 +0100 Subject: [PATCH 1/2] first training --- test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test.py b/test.py index d1f4996..e78de7d 100644 --- a/test.py +++ b/test.py @@ -17,19 +17,19 @@ # ---------------------- # - read the input data: -''' +# ''' import mnist_loader training_data, validation_data, test_data = mnist_loader.load_data_wrapper() training_data = list(training_data) -''' +# ''' # --------------------- # - network.py example: -#import network +import network -''' +# ''' net = network.Network([784, 30, 10]) -net.SGD(training_data, 30, 10, 3.0, test_data=test_data) -''' +net.SGD(training_data, 10, 10, 3.0, test_data=test_data) +# ''' # ---------------------- # - network2.py example: From fd3dd3c1b5b43001fe1d4b65f15d01d8deb97a5a Mon Sep 17 00:00:00 2001 From: YLI Date: Fri, 1 Mar 2019 13:22:02 +0100 Subject: [PATCH 2/2] other folders added --- NeuralNetwork/LinearUnit.py | 32 +++++++++++++++++ NeuralNetwork/Perceptron.py | 33 ++++++++++++++++++ NeuralNetwork/and.py | 28 +++++++++++++++ NeuralNetwork/main.py | 17 +++++++++ NeuralNetwork/trial.py | 12 +++++++ SolarEarth/Earth.py | 50 +++++++++++++++++++++++++++ SolarEarth/SolarSystem.py | 69 +++++++++++++++++++++++++++++++++++++ test.py | 10 +++--- 8 files changed, 246 insertions(+), 5 deletions(-) create mode 100644 NeuralNetwork/LinearUnit.py create mode 100644 NeuralNetwork/Perceptron.py create mode 100644 NeuralNetwork/and.py create mode 100644 NeuralNetwork/main.py create mode 100644 NeuralNetwork/trial.py create mode 100644 SolarEarth/Earth.py create mode 100644 SolarEarth/SolarSystem.py diff --git a/NeuralNetwork/LinearUnit.py b/NeuralNetwork/LinearUnit.py new file mode 100644 index 0000000..2a5c971 --- /dev/null +++ b/NeuralNetwork/LinearUnit.py @@ -0,0 +1,32 @@ +from Perceptron import Perceptron + +f = lambda x: x + + +class LinearUnit(Perceptron): + def __init__(self, input_num): + Perceptron.__init__(self, input_num, f) + + +def get_training_dataset(): + input_vecs = [[5], [3], [8], [1.4], [10.1]] + labels = [5500, 2300, 7600, 1800, 11400] + return input_vecs, labels + + +def train_linear_unit(): + lu = LinearUnit(1) + input_vecs, labels = get_training_dataset() + lu.train(input_vecs, labels, 20, 0.01) + return lu + + +if __name__ == '__main__': + linear_unit = train_linear_unit() + + print(linear_unit) + + print('Work 3.4 years, monthly salary = %.2f' % linear_unit.predict([3.4])) + print('Work 15 years, monthly salary = %.2f' % linear_unit.predict([15])) + print('Work 1.5 years, monthly salary = %.2f' % linear_unit.predict([1.5])) + print('Work 6.3 years, monthly salary = %.2f' % linear_unit.predict([6.3])) diff --git a/NeuralNetwork/Perceptron.py b/NeuralNetwork/Perceptron.py new file mode 100644 index 0000000..f55b669 --- /dev/null +++ b/NeuralNetwork/Perceptron.py @@ -0,0 +1,33 @@ +from functools import reduce + + +class Perceptron(object): + def __init__(self, input_num, activator): + self.activator = activator + self.weights = [0.0 for _ in range(input_num)] + self.bias = 0.0 + + def __str__(self): + return 'weights\t:%s\nbias\t:%f\n' % (self.weights, self.bias) + + def predict(self, input_vec): + return self.activator( + reduce(lambda a, b: a + b, + map(lambda x: x[0] * x[1], + zip(input_vec, self.weights))) + self.bias) + + def train(self, input_vecs, labels, iteration, rate): + for i in range(iteration): + self._one_iteration(input_vecs, labels, rate) + + def _one_iteration(self, input_vecs, labels, rate): + samples = zip(input_vecs, labels) + for (input_vec, label) in samples: + output = self.predict(input_vec) + self._update_weights(input_vec, output, label, rate) + + def _update_weights(self, input_vec, output, label, rate): + delta = label - output + self.weights = list(map(lambda x: x[1] + rate * delta * x[0], + zip(input_vec, self.weights))) + self.bias += rate * delta; diff --git a/NeuralNetwork/and.py b/NeuralNetwork/and.py new file mode 100644 index 0000000..bf1f1a8 --- /dev/null +++ b/NeuralNetwork/and.py @@ -0,0 +1,28 @@ +from NeuralNetwork.Perceptron import Perceptron + + +def f(x): + return 1 if x > 0 else 0 + + +def get_training_dataset(): + input_vecs = [[1, 1], [0, 0], [1, 0], [0, 1]] + labels = [1, 0, 0, 0] + return input_vecs, labels + + +def train_and_perceptron(): + p = Perceptron(2, f) + input_vecs, labels = get_training_dataset() + p.train(input_vecs, labels, 4, 0.1) + return p + + +if __name__ == '__main__': + and_perception = train_and_perceptron() + print(and_perception) + ## + print('1 and 1 = %d' % and_perception.predict([1, 1])) + print('0 and 1 = %d' % and_perception.predict([0, 1])) + print('1 and 0 = %d' % and_perception.predict([1, 0])) + print('0 and 0 = %d' % and_perception.predict([0, 0])) diff --git a/NeuralNetwork/main.py b/NeuralNetwork/main.py new file mode 100644 index 0000000..6597349 --- /dev/null +++ b/NeuralNetwork/main.py @@ -0,0 +1,17 @@ +def output(n): + print('lala %d' % n) + + +def addW(x): + return x[0] * x[1] + + +input_vecs = [[1, 1], [0, 0], [1, 0], [0, 1]] +labels = [1, 0, 0, 0] +weights = [0, 0] +samples = zip(input_vecs, labels) +for (input_vec, label) in samples: + vec = zip(input_vec, weights) + print(input_vec, weights, list(vec)) + weights = list(map(addW, [[1, 2], [2, 3]])) + print(list(weights)) diff --git a/NeuralNetwork/trial.py b/NeuralNetwork/trial.py new file mode 100644 index 0000000..9058f98 --- /dev/null +++ b/NeuralNetwork/trial.py @@ -0,0 +1,12 @@ +import numpy as np + +# float_formatter = lambda x: "%.2f" % x +# np.set_printoptions(formatter={'float_kind': float_formatter}) +input = [2, 3, 1] +str = [np.random.randn(y, 1).tolist() for y in input] +print(str) +# print([y for y in input]) +# print(list(zip(input[:-1], input[1:]))) +# print([np.random.randn(y, x).tolist() for x, y in zip(input[:-1], input[1:])]) +# print(input[:-1]) +# print(input[1:]) diff --git a/SolarEarth/Earth.py b/SolarEarth/Earth.py new file mode 100644 index 0000000..37cae0f --- /dev/null +++ b/SolarEarth/Earth.py @@ -0,0 +1,50 @@ +import numpy as np + +radius = 100 +GM = 100 # v = sqrt(GM/r) +d_theta = -0.1 + + +class Earth: + def __init__(self, canvas, sun, v_init): + self.canvas = canvas + # list comprehension, give the center coord of the sun + self.sun_pos = np.array([coord + 10 for coord in canvas.coords(sun)][0:2]) + self.cur_pos = self.sun_pos - [0, radius] + self.old_pos = self.cur_pos[:] + self.id = canvas.create_oval((self.cur_pos - 10).tolist(), + (self.cur_pos + 10).tolist(), + fill='blue') + self.theta = 0 + self.v = np.array(v_init) # initial speed + + def __getitem__(self, item): + # return the current earth coordinate + return self.cur_pos[item] + + def orbit(self): + self.theta += d_theta + self.cur_pos = np.array([self.sun_pos[0] + radius * np.sin(self.theta), + self.sun_pos[1] - radius * np.cos(self.theta)]) + self.update_pos() + + def dynamic_orbit(self, dt): + r_vector = self.cur_pos - self.sun_pos + r = np.linalg.norm(r_vector) + self.a = - GM / r ** 3 * r_vector # acceleration + self.v = self.v + self.a * dt + + self.cur_pos = self.cur_pos + self.v * dt + self.update_pos() + + def update_pos(self): + dx, dy = self.cur_pos - self.old_pos + self.canvas.move(self.id, dx, dy) + self.canvas.create_line(self.old_pos.tolist(), self.cur_pos.tolist(), dash=(3, 5), tags='line_tag') + self.old_pos = self.cur_pos + + def __del__(self): + # self.cur_pos = self.sun_pos - np.array([0, radius]) + # self.update_pos() + self.canvas.delete(self.id) + self.canvas.delete('line_tag') diff --git a/SolarEarth/SolarSystem.py b/SolarEarth/SolarSystem.py new file mode 100644 index 0000000..f00821c --- /dev/null +++ b/SolarEarth/SolarSystem.py @@ -0,0 +1,69 @@ +from tkinter import * +from SolarEarth.Earth import Earth +import time + + +def btn_reaction(): + if btn_text.get() == 'Run': + btn_text.set('Pause') + else: + btn_text.set('Run') + + while btn_text.get() == 'Pause': + # earth.orbit() + earth.dynamic_orbit(10) + pos_text.set('X:{0:9.1f}, Y:{1:9.1f}'.format(earth[0], earth[1])) + root.update() + time.sleep(0.1) + + +def btn_reset(): + global earth + del earth + earth = Earth(canvas, sun, [float(vx.get()), float(vy.get())]) + + +root = Tk() +root.title('Solar System') + +# frame for animation +frm_L = Frame(root) +canvas = Canvas(frm_L, width=800, height=600) +canvas.pack() +frm_L.pack(side=LEFT) + +# frame for info display +frm_R = Frame(root, bd=2, relief=SUNKEN) +Label(frm_R, text='Initial speed:').pack(pady=5, anchor='w') +frm_R1 = Frame(frm_R) +Label(frm_R1, text='Vx:').pack(side=LEFT) +vx = StringVar() +Entry(frm_R1, textvariable=vx).pack() +frm_R1.pack() +frm_R2 = Frame(frm_R) +Label(frm_R2, text='Vy:').pack(side=LEFT) +vy = StringVar() +Entry(frm_R2, textvariable=vy).pack(side=LEFT) +frm_R2.pack() + +Label(frm_R, text='Earth Coordinates:').pack(pady=5, anchor='w') +pos_text = StringVar() +Label(frm_R, textvariable=pos_text).pack(pady=5, anchor='w') +btn_text = StringVar() +Button(frm_R, textvariable=btn_text, command=btn_reaction).pack(pady=15, side=BOTTOM) +Button(frm_R, text='Reset', command=lambda: btn_reset()).pack() +frm_R.pack(side=RIGHT) + +root.update() + +sun = canvas.create_oval(canvas.winfo_width() / 2 - 10, canvas.winfo_height() / 2 - 10, + canvas.winfo_width() / 2 + 10, canvas.winfo_height() / 2 + 10, + fill='red') +vx.set('-1') +vy.set('0') +earth = Earth(canvas, sun, [float(vx.get()), float(vy.get())]) +pos_text.set('X:{0:9.1f}, Y:{1:9.1f}'.format(earth[0], earth[1])) +btn_text.set('Run') +# btn_reaction() + +root.mainloop() diff --git a/test.py b/test.py index e78de7d..d0fbecb 100644 --- a/test.py +++ b/test.py @@ -17,19 +17,19 @@ # ---------------------- # - read the input data: -# ''' +''' import mnist_loader training_data, validation_data, test_data = mnist_loader.load_data_wrapper() training_data = list(training_data) -# ''' +''' # --------------------- # - network.py example: -import network +# import network -# ''' +''' net = network.Network([784, 30, 10]) net.SGD(training_data, 10, 10, 3.0, test_data=test_data) -# ''' +''' # ---------------------- # - network2.py example: