#------------------------------------------------------------------------------ # Simulazione singolo neurone con doppio ingresso. # By Claudio Fin 2016 #------------------------------------------------------------------------------ try: import Tkinter as tk except ImportError: import tkinter as tk from math import exp #--------------------------------------------------------------------- PUNTI = 18 BIMG = ('R0lGODdh4ABJAOfRAAYGBggICAkJCQoKCgsLCwwMDA4ODg8PDxAQEBERERISE' 'hMTExUVFRYWFhcXFxgYGBkZGRoaGhsbGxwcHB4eHh8fHyAgICEhISIiIiMjIy' 'QkJCUlJSYmJicnJygoKCkpKSoqKisrKywsLC4uLjAwMDIyMjMzMzQ0NDU1NTY' '2Njc3Nzg4ODk5OTo6Ojs7Ozw8PD09PT4+Pj8/P0BAQEFBQUJCQkREREVFRUZG' 'RkdHR0hISEpKSkxMTE1NTU5OTk9PT1BQUFFRUVJSUlNTU1RUVFZWVldXV1hYW' 'FlZWVpaWltbW1xcXF1dXV5eXmBgYGFhYWJiYmNjY2VlZWZmZmdnZ2lpaWtra2' '1tbW5ubm9vb3BwcHFxcXNzc3R0dHV1dXZ2dnd3d3h4eHl5eXp6ent7e319fX9' '/f4GBgYODg4SEhIWFhYaGhoeHh4mJiYqKiouLi4yMjI2NjY6Ojo+Pj5GRkZOT' 'k5SUlJWVlZaWlpeXl5mZmZqampubm5ycnJ2dnZ6enp+fn6CgoKGhoaOjo6Wlp' 'aampqenp6ioqKmpqaqqqqurq62tra+vr7CwsLGxsbOzs7W1tba2tre3t7i4uL' 'm5ubq6uru7u729vb+/v8DAwMHBwcLCwsPDw8TExMXFxcbGxsfHx8jIyMnJycv' 'Ly83Nzc/Pz9DQ0NHR0dPT09XV1dbW1tfX19jY2NnZ2dra2tvb29zc3N3d3d7e' '3t/f3+Dg4OHh4eLi4uPj4+Tk5OXl5ebm5ufn5+jo6Onp6evr6+zs7O7u7u/v7' '/Dw8PHx8fLy8vPz8/T09PX19fb29vf39/j4+Pn5+fr6+vv7+/z8/P39/f7+/v' '/////////////////////////////////////////////////////////////' '/////////////////////////////////////////////////////////////' '/////////////////////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GEgRBEkLXX4c9dstlMw' 'Sz/kszt2SICQTB4QPdApEhNtl5L+QxyCKzJAwDkwhEzAaLRPOHFAKpIHYeSie' 'uUMMPR6xSJRNA4QUHXgikywRU0LCC2LJgMEUjniNEsMEnIawwS8FMckjExyyi' 'Ci3FMiMKLLUj1O+/ASePlr346uv8U8VIAoYKVMC7krzT/1T99QGoAIYkxJSrE' 'rq4d0/T9yqEP34xBFmLrbbq18S++5LALyyxKB2bbP0vuZ/48tcQWdHKVgBkiQ' 'DfF5FPhYokoypVAkuyQAJWhFGOAgmkJDVBkVRQf3ri00j81EGQfPAjaFITSNi' 'UwRJa5IRUshKWtOTCi8AwSEMq0pFqOJEbsshF7i2kiIxoxMOH+PAlF8rQhjoU' 'xHUpgweoEEgnhMCsH2QgCImQyBFpgh/98Mc/HDzaIUzQDGRsABLRiMRhNpEAW' 'TRkiziZTnXM5ZDsbEeCZRtCGrTgBIMQgQ4JgWNPXhOb2aQvGrfJzW7+57leRI' 'ACwijILyTwQLcNEIRnUQxjHAMZyVDGMpjRTPdusQASTGkgyajBGQQmF7rYBS9' '6SWAO3EAEvEWDGTzAQhFdtocVPMORUWxGEKawS47VogGsEIghTuAMNgCgCEYw' 'giCKSS9aRHEgnRiGLBKECdFR85vgDKc4x0nOcprznOhMpzrDGRAAOw==') #--------------------------------------------------------------------- class CUBE3D(object): def __init__(self, canvas, x, y, wid, zmin, zmax): self.canvas = canvas self.x = x self.y = y self.wid = wid self.zmin = zmin self.zmax = zmax self.gitm = [] def _rear_box(self): x, y, wid, dw = self.x, self.y, self.wid, self.wid // 2 self.gitm.append(self.canvas.create_line( x+dw, y-dw, x+dw+wid-1, y-dw, fill='#00aa00', dash=4)) # diet bas self.gitm.append(self.canvas.create_line( x, y, x+dw, y-dw, fill='blue', arrow='last')) # diag bas sx self.gitm.append(self.canvas.create_line( x+dw, y-dw, x+dw, y-dw-wid+1, fill='#00aa00', dash=4)) # diet sx def _front_box(self): x, y, wid, dw = self.x, self.y, self.wid, self.wid // 2 self.gitm.append(self.canvas.create_line( x+dw+wid-1, y-dw-wid+1, x+dw+wid-1, y-dw, fill='#00aa00', dash=4)) # diet dx self.gitm.append(self.canvas.create_line( x+wid-1, y-wid+1, x, y-wid+1, fill='#00aa00', dash=4)) self.gitm.append(self.canvas.create_line( x+wid-1, y-wid+1, x+dw+wid-1, y-dw-wid+1, fill='#00aa00', dash=4)) self.gitm.append(self.canvas.create_line( x+wid-1, y-wid+1, x+wid-1, y, fill='#00aa00', dash=4)) self.gitm.append(self.canvas.create_line( x+dw+wid-1, y-dw-wid+1, x+dw, y-dw-wid+1, fill='#00aa00', dash=4)) #diet alt self.gitm.append(self.canvas.create_line( x, y-wid+1, x+dw, y-dw-wid+1, fill='#00aa00', dash=4)) #diag alt sx self.gitm.append(self.canvas.create_line( x+wid-1, y, x+dw+wid-1, y-dw, fill='#00aa00', dash=4)) #diag bas dx self.gitm.append(self.canvas.create_line( x, y, x+wid-1, y, fill='blue', arrow='last')) # dav bas self.gitm.append(self.canvas.create_line( x, y, x, y-wid+1, fill='blue', arrow='last')) # dav sx def _translate(self, z): return self.y + z / (self.zmax-self.zmin) * (-self.wid + 1.0) def draw(self, data): for itm in self.gitm: self.canvas.delete(itm) self.gitm = [] self._rear_box() nrow = len(data) ncol = len(data[0]) dw = self.wid // 2 for y in range(nrow-1, 0, -1): for x in range(ncol-1): ddw = dw * y / (ncol - 1.0) x1 = int(self.x + self.wid * x / (ncol - 1) - 1.0) + ddw y1 = int(self._translate(data[y][x])) - ddw x2 = int(self.x + self.wid * (x+1) / (ncol - 1) - 1.0) + ddw y2 = int(self._translate(data[y][x+1])) - ddw ddw = dw * (y-1) / (ncol - 1.0) x3 = int(self.x + self.wid * (x+1) / (ncol - 1) - 1.0) + ddw y3 = int(self._translate(data[y-1][x+1])) - ddw x4 = int(self.x + self.wid * x / (ncol - 1) - 1.0) + ddw y4 = int(self._translate(data[y-1][x])) - ddw self.gitm.append(self.canvas.create_polygon( x1, y1, x2, y2, x3, y3, x4, y4, fill='red', outline='#cfcf00')) self._front_box() #--------------------------------------------------------------------- class GUI(object): def __init__(self): self.root = tk.Tk() self.root.resizable(0, 0) self.root.title('Two input neuron - By C.Fin 2016') self.canvas = tk.Canvas(self.root, width=278, height=378, relief="sunken", border=1, bg='white', highlightthickness=0) self.canvas.pack(side='left', padx=8, pady=8) self.img = tk.PhotoImage(data=BIMG) self.canvas.create_image(140, 328, image=self.img, anchor='center') self.frameDestra = tk.Frame(self.root) self.frameDestra.pack(side='left', fill='y', expand=1, pady=8, padx=8) tk.Label(self.frameDestra, text="Peso\nwx1").grid(row=0, column=0) tk.Label(self.frameDestra, text="Peso\nwx2").grid(row=0, column=1) tk.Label(self.frameDestra, text="Peso\nbias").grid(row=0,column=2) self.wx1Scale = tk.Scale( self.frameDestra, from_=20., to=-20., orient="vertical", showvalue=0, length=180, resolution=0.01) self.wx1Scale.grid(row=1, column=0) self.wx2Scale = tk.Scale( self.frameDestra, from_=20., to=-20., orient="vertical", showvalue=0, length=180, resolution=0.01) self.wx2Scale.grid(row=1, column=1) self.biasScale = tk.Scale( self.frameDestra, from_=20., to=-20., orient="vertical", showvalue=0, length=180, resolution=0.01) self.biasScale.grid(row=1, column=2) self.labelwx1 = tk.Label(self.frameDestra, width=6) self.labelwx1.grid(row=2, column=0) self.labelwx2 = tk.Label(self.frameDestra, width=6) self.labelwx2.grid(row=2, column=1) self.labelBias = tk.Label(self.frameDestra, width=6) self.labelBias.grid(row=2, column=2) self.buttonReset = tk.Button( self.frameDestra, text="Reset", width=10) self.buttonReset.grid(row=3, column=0, columnspan=3, pady=48) #--------------------------------------------------------------------- class App(object): def __init__(self): self.gui = GUI() self.c3d = CUBE3D(self.gui.canvas, 6, 273, 180, 0.0, 1.0) self.gui.biasScale.configure(command=self.draw) self.gui.wx1Scale.configure(command=self.draw) self.gui.wx2Scale.configure(command=self.draw) self.gui.buttonReset.configure(command=self.reset) self.data = [[0] * PUNTI for _ in range(PUNTI)] def reset(self): self.gui.wx1Scale.set(0) self.gui.wx2Scale.set(0) self.gui.biasScale.set(0) def draw(self, ev=None): self.gui.labelwx1.configure(text="%5.2f" % self.gui.wx1Scale.get()) self.gui.labelwx2.configure(text="%5.2f" % self.gui.wx2Scale.get()) self.gui.labelBias.configure(text="%5.2f" % self.gui.biasScale.get()) step = 1.0 / (PUNTI - 1) for y in range(PUNTI): for x in range(PUNTI): a = (self.gui.wx1Scale.get() * x*step + self.gui.wx2Scale.get() * y*step + self.gui.biasScale.get()) self.data[y][x] = 1.0 / ( 1.0 + exp(-a) ) self.c3d.draw(self.data) def start(self): self.gui.root.mainloop() #--------------------------------------------------------------------- app = App() app.start()