Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strongly improved network stripping #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions ocrolib/lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ def __init__(self,Nh,No,initial_range=initial_range,rand=rand):
self.No = No
self.W2 = randu(No,Nh+1)*initial_range
self.DW2 = zeros((No,Nh+1))
def postLoad(self):
self.DW2 = zeros(self.W2.shape)

def preSave(self):
for var in ('state', 'DW2'):
if hasattr(self, var):
delattr(self, var)

def ninputs(self):
return self.Nh
def noutputs(self):
Expand Down Expand Up @@ -596,6 +604,12 @@ def walk(self):
yield self
for sub in self.nets:
for x in sub.walk(): yield x
def preSave(self):
self.dstats = defaultdict(list) # reset
for delta in ('deltas', 'ldeltas'):
if hasattr(self, delta):
delattr(self, delta)

def ninputs(self):
return self.nets[0].ninputs()
def noutputs(self):
Expand Down Expand Up @@ -859,11 +873,16 @@ def __init__(self,ninput,nstates,noutput=-1,codec=None,normalize=normalize_nfkc)
self.clear_log()
def walk(self):
for x in self.lstm.walk(): yield x
def clear_log(self):
def clear_log(self, deallocate_tempvars=False):
self.command_log = []
self.error_log = []
self.cerror_log = []
self.key_log = []
if deallocate_tempvars:
for attrname in ('outputs', 'targets', 'aligned'):
if hasattr(self, attrname):
delattr(self, attrname)

def __setstate__(self,state):
self.__dict__.update(state)
self.upgrade()
Expand All @@ -874,7 +893,7 @@ def upgrade(self):
if "cerror_log" not in dir(self): self.cerror_log = []
if "key_log" not in dir(self): self.key_log = []
def info(self):
self.net.info()
self.lstm.info()
def setLearningRate(self,r,momentum=0.9):
self.lstm.setLearningRate(r,momentum)
def predictSequence(self,xs):
Expand Down
3 changes: 2 additions & 1 deletion ocropus-rtrain
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ def save_lstm(fname,network):
network.lstm.save(fname)
else:
if args.strip:
network.clear_log()
print yellow('saving stripped network (without temporary variables)...')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No.

network.clear_log(deallocate_tempvars=True)
for x in network.walk(): x.preSave()
ocrolib.save_object(fname,network)
if args.strip:
Expand Down