<s> import optparse <EOL> import os , struct <EOL> import cPickle as pickle <EOL> import dataLoader as dl <EOL> import brnnet as rnnet <EOL> import numpy as np <EOL> def writeUttHeader ( fid , key , uttSize , numClasses ) : <EOL> """<STR_LIT>""" <EOL> fid . write ( key + '<STR_LIT:U+0020>' ) <EOL> dat = struct . pack ( '<STR_LIT:b>' , <NUM_LIT:0> ) <EOL> fid . write ( dat ) <EOL> fid . write ( '<STR_LIT>' ) <EOL> floatsize = struct . pack ( '<STR_LIT:b>' , <NUM_LIT:4> ) <EOL> fid . write ( floatsize ) <EOL> dat = struct . pack ( '<STR_LIT:i>' , uttSize ) <EOL> fid . write ( dat ) <EOL> fid . write ( floatsize ) <EOL> dat = struct . pack ( '<STR_LIT:i>' , numClasses ) <EOL> fid . write ( dat ) <EOL> def writeLogLikes ( loader , nn , fn , outDir , writePickle = False ) : <EOL> data_dict , alis , keys , sizes = loader . loadDataFileDict ( fn ) <EOL> fid = open ( outDir + '<STR_LIT>' % fn , '<STR_LIT:w>' ) <EOL> lik_dict = dict ( ) <EOL> print "<STR_LIT>" % fn <EOL> for i , k in enumerate ( keys ) : <EOL> assert data_dict [ k ] . shape [ <NUM_LIT:1> ] < nn . maxBatch , "<STR_LIT>" <EOL> writeUttHeader ( fid , k , sizes [ i ] , nn . outputDim ) <EOL> probs = nn . costAndGrad ( data_dict [ k ] ) <EOL> assert probs . dtype == np . float32 , "<STR_LIT>" <EOL> assert probs . shape [ <NUM_LIT:0> ] == nn . outputDim , "<STR_LIT>" <EOL> probs = np . log ( probs ) <EOL> probs . T . tofile ( fid ) <EOL> lik_dict [ k ] = probs <EOL> fid . close ( ) <EOL> if writePickle : <EOL> print "<STR_LIT>" % fn <EOL> with open ( outDir + '<STR_LIT>' % fn , '<STR_LIT:wb>' ) as f : <EOL> pickle . dump ( lik_dict , f ) <EOL> print "<STR_LIT>" % fn <EOL> return lik_dict <EOL> def run ( args = None ) : <EOL> usage = "<STR_LIT>" <EOL> parser = optparse . OptionParser ( usage = usage ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> default = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:10> ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT> ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> default = None , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> default = None , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> default = None , help = "<STR_LIT>" ) <EOL> ( opts , args ) = parser . parse_args ( args ) <EOL> with open ( opts . inFile , '<STR_LIT:r>' ) as fid : <EOL> old_opts = pickle . load ( fid ) <EOL> _ = pickle . load ( fid ) <EOL> loader = dl . DataLoader ( opts . dataDir , old_opts . rawDim , old_opts . inputDim , load_ali = False ) <EOL> nn = rnnet . NNet ( old_opts . inputDim , old_opts . outputDim , <EOL> old_opts . layerSize , old_opts . numLayers , opts . maxUttLen , <EOL> temporalLayer = old_opts . temporalLayer , train = False ) <EOL> nn . initParams ( ) <EOL> nn . fromFile ( fid ) <EOL> for i in range ( <NUM_LIT:1> , opts . numFiles + <NUM_LIT:1> ) : <EOL> writeLogLikes ( loader , nn , i , opts . outFile , opts . writePickle ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run ( ) </s>
