Skip to content

Instantly share code, notes, and snippets.

@hussius
Created December 18, 2015 12:17
Show Gist options
  • Save hussius/d842f6fd32dc42ab3665 to your computer and use it in GitHub Desktop.
Save hussius/d842f6fd32dc42ab3665 to your computer and use it in GitHub Desktop.
import sys
f = open(sys.argv[1])
wts = [] # This will be a list of list of lists (=list of matrices) with weights
bias_in = [] # List of lists of bias values in the input part of each sublayer
bias_out = [] # List of lists of bias values in the output part of each sublayer
prefs = [] # Prefixes for file names
state = None # state can be None, 'weight', 'bias_in' or 'bias_out'
for line in f:
if 'layer' in line:
prefs.append('_'.join(line.strip().split()))
continue
if 'weight' in line:
current_layer_weights = [] # initialise list of weight lists
if state != None: bias_in.append(current_vbias)
state = 'weight'
#print state
elif 'bias' in line:
if 'hidden bias' in line:
current_hbias = []
wts.append(current_layer_weights)
state = 'bias_out'
if 'visible bias' in line:
current_vbias = []
bias_out.append(current_hbias)
state = 'bias_in'
#print state
else: # no state change
if state == 'weight':
current_layer_weights.append(line.strip())
elif state == 'bias_in':
current_vbias.append(line.strip())
else:
current_hbias.append(line.strip())
bias_in.append(current_vbias)
for i in range(0,len(prefs)):
hb_file = open(prefs[i]+'_hidden_bias.txt','w')
vb_file = open(prefs[i]+'_visible_bias.txt','w')
for b in bias_in[i]:
vb_file.write(b+'\t')
for b in bias_out[i]:
hb_file.write(b+'\t')
hb_file.close()
vb_file.close()
w_file = open(prefs[i]+'_weights.txt','w')
for w in wts[i]:
w_file.write(w+'\n')
w_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment