From cccccbf6cca94a3eaf813b4468453160e91c332b Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Mon, 14 Apr 2014 08:14:45 +0800 Subject: First commit --- NetGen/layerobj.py | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++ NetGen/layerobj.pyc | Bin 0 -> 8454 bytes NetGen/mathobj.py | 32 +++++++++++ NetGen/mathobj.pyc | Bin 0 -> 1177 bytes NetGen/testscript.py | 72 +++++++++++++++++++++++ 5 files changed, 264 insertions(+) create mode 100644 NetGen/layerobj.py create mode 100644 NetGen/layerobj.pyc create mode 100644 NetGen/mathobj.py create mode 100644 NetGen/mathobj.pyc create mode 100644 NetGen/testscript.py (limited to 'NetGen') diff --git a/NetGen/layerobj.py b/NetGen/layerobj.py new file mode 100644 index 0000000..3cfadf0 --- /dev/null +++ b/NetGen/layerobj.py @@ -0,0 +1,160 @@ +''' +Created on Mar 16, 2014 + +@author: joe +''' + +import random +import mathobj + +matRandFnc=lambda :3*random.gauss(0.0,1.0) +vecRandFnc=lambda :random.random()/5.0-4.1 + +class SharedLinearity: + def __init__(self,nIn=1,nOut=1,inS=True, + matRand=matRandFnc,vecRand=vecRandFnc): + self.nInputs=nIn + self.nOutputs=nOut + self.inScale=inS + self.matRand=matRand + self.vecRand=vecRand + + def flush(self): + print "", self.nOutputs, self.nInputs + mathobj.randomFnc=self.matRand + mathobj.inputScale=self.inScale + mathobj.PrintTransMatrix(self.nInputs, self.nOutputs) + mathobj.randomFnc=self.vecRand + mathobj.PrintVector(self.nOutputs) + +class Linearity: + def __init__(self,nIn=1,nOut=1,inS=True, + matRand=matRandFnc): + self.nInputs=nIn + self.nOutputs=nOut + self.inScale=inS + self.matRand=matRand + + def flush(self): + print "", self.nOutputs, self.nInputs + mathobj.randomFnc=self.matRand + mathobj.inputScale=self.inScale + mathobj.PrintTransMatrix(self.nInputs, self.nOutputs) + +class UpdatableBias: + def __init__(self,nIn=1,vecRand=vecRandFnc): + self.nInputs=nIn + self.nOutputs=nIn + self.vecRand=vecRand + + def flush(self): + print "", self.nOutputs, self.nInputs + mathobj.randomFnc=self.vecRand + mathobj.PrintVector(self.nInputs) + +class MiscPipe: + def __init__(self,nIn=1,name=""): + self.nInputs=nIn + self.nOutputs=nIn + self.name=name + + def flush(self): + print self.name, self.nOutputs, self.nInputs + +class Distrib: + def __init__(self,nIn=1,size=1): + self.nInputs=nIn + self.nOutputs=nIn + self.size=size + + def flush(self): + print "", self.nOutputs, self.nInputs + print self.size + +class Combine: + def __init__(self,nIn=1,size=1): + self.nInputs=nIn + self.nOutputs=nIn + self.size=size + + def flush(self): + print "", self.nOutputs, self.nInputs + print self.size + +class Divide: + def __init__(self,nIn=1,divLen=[]): + self.nInputs=nIn + self.nOutputs=divLen[0] + self.divLen=divLen + + def push(self,nxtLen): + self.divLen+=[nxtLen]; + + def flush(self): + print "", self.nOutputs, self.nInputs + print len(self.divLen), + for Len in self.divLen: + print Len, + print + +class Merge: + def __init__(self,nOut=1,divLen=[]): + self.nOutputs=nOut + self.nInputs=divLen[0] + self.divLen=divLen + + def push(self,nxtLen): + self.divLen+=[nxtLen]; + + def flush(self): + print "", self.nOutputs, self.nInputs + print len(self.divLen), + for Len in self.divLen: + print Len, + print + +class Reorder: + def __init__(self,nIn=1,Order=[]): + self.nInputs=nIn + self.nOutputs=nIn + self.Order=Order + + def push(self,nxtPos): + self.Order+=[nxtPos]; + + def flush(self): + print "", self.nOutputs, self.nInputs + print len(self.Order), + for Len in self.Order: + print Len, + print + +class Compound: + def __init__(self,nIn=1,nOut=1,Objs=[]): + self.nInputs=nIn + self.nOutputs=nOut + self.Objs=Objs + + def push(self,nxtObj): + self.Objs+=[nxtObj]; + + def flush(self): + print "", self.nOutputs, self.nInputs + for Obj in self.Objs: + Obj.flush() + print "" + +class Discrete: + def __init__(self,nIn=1,nOut=1,Objs=[]): + self.nInputs=nIn + self.nOutputs=nOut + self.Objs=Objs + + def push(self,nxtObj): + self.Objs+=[nxtObj]; + + def flush(self): + print "", self.nOutputs, self.nInputs + for Obj in self.Objs: + Obj.flush() + print "" \ No newline at end of file diff --git a/NetGen/layerobj.pyc b/NetGen/layerobj.pyc new file mode 100644 index 0000000..1ad80a9 Binary files /dev/null and b/NetGen/layerobj.pyc differ diff --git a/NetGen/mathobj.py b/NetGen/mathobj.py new file mode 100644 index 0000000..77dad90 --- /dev/null +++ b/NetGen/mathobj.py @@ -0,0 +1,32 @@ +''' +Created on Mar 16, 2014 + +@author: joe +''' + +import math, random + + +randomFnc=lambda :3*random.gauss(0.0,1.0) +inputScale=True + +def PrintTransMatrix(rows,cols): + PrintMatrix(cols,rows) + +def PrintMatrix(rows,cols): + print 'm', rows, cols + for row in range(rows): + for col in range(cols): + if(inputScale): + print randomFnc()/math.sqrt(rows), + else: + print randomFnc(), + print + +def PrintVector(cols): + print 'v', cols + for col in range(cols): + print randomFnc(), + print + + \ No newline at end of file diff --git a/NetGen/mathobj.pyc b/NetGen/mathobj.pyc new file mode 100644 index 0000000..4963b50 Binary files /dev/null and b/NetGen/mathobj.pyc differ diff --git a/NetGen/testscript.py b/NetGen/testscript.py new file mode 100644 index 0000000..70fa005 --- /dev/null +++ b/NetGen/testscript.py @@ -0,0 +1,72 @@ +''' +Created on Mar 16, 2014 + +@author: joe +''' +from layerobj import * + +if __name__ == '__main__': + Discrete(1,1,[Distrib(1,3)]).flush(); + Discrete(1,1, + [SharedLinearity(1,1), + MiscPipe(1,""), + MiscPipe(1,""), + ]).flush(); + Discrete(1,1, + [MiscPipe(1,""), + MiscPipe(1), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [SharedLinearity(1,1), + MiscPipe(1), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [MiscPipe(1,""), + MiscPipe(1), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [MiscPipe(1,""), + MiscPipe(1), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [Distrib(1,2), + MiscPipe(1), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [MiscPipe(1,""), + SharedLinearity(1,1), + SharedLinearity(1,1), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [MiscPipe(1), + Combine(2), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [MiscPipe(1), + SharedLinearity(1,1), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [MiscPipe(1), + MiscPipe(1,""), + MiscPipe(1), + ]).flush(); + Discrete(1,1, + [SharedLinearity(1,1), + SharedLinearity(1,1), + SharedLinearity(1,1), + ]).flush(); + Discrete(1,1, + [Combine(3) + ]).flush(); + MiscPipe(1,"").flush() + SharedLinearity(1,1).flush(); + MiscPipe(1,"").flush(); + \ No newline at end of file -- cgit v1.2.3-70-g09d2