#NEWLINE# def transform_batch( self , batch ) : #NEWLINE# #INDENT# batch_with_masks = [ ] #NEWLINE# for ( i , ( source , source_batch ) ) in enumerate ( zip ( self . data_stream . sources , batch ) ) : #NEWLINE# #INDENT# if ( source not in self . mask_sources ) : #NEWLINE# #INDENT# batch_with_masks . append ( source_batch ) #NEWLINE# continue #NEWLINE# #UNINDENT# shapes = [ numpy . asarray ( sample ) . shape for sample in source_batch ] #NEWLINE# lengths = [ shape [ 0 ] for shape in shapes ] #NEWLINE# max_sequence_length = max ( lengths ) #NEWLINE# rest_shape = shapes [ 0 ] [ 1 : ] #NEWLINE# if ( not all ( [ ( shape [ 1 : ] == rest_shape ) for shape in shapes ] ) ) : #NEWLINE# #INDENT# raise ValueError ( 'All dimensions except length must be equal' ) #NEWLINE# #UNINDENT# dtype = numpy . asarray ( source_batch [ 0 ] ) . dtype #NEWLINE# padded_batch = numpy . zeros ( ( ( len ( source_batch ) , max_sequence_length ) + rest_shape ) , dtype = dtype ) #NEWLINE# for ( i , sample ) in enumerate ( source_batch ) : #NEWLINE# #INDENT# padded_batch [ i , : len ( sample ) ] = sample #NEWLINE# #UNINDENT# batch_with_masks . append ( padded_batch ) #NEWLINE# mask = numpy . zeros ( ( len ( source_batch ) , max_sequence_length ) , self . mask_dtype ) #NEWLINE# for ( i , sequence_length ) in enumerate ( lengths ) : #NEWLINE# #INDENT# mask [ i , : sequence_length ] = 1 #NEWLINE# #UNINDENT# batch_with_masks . append ( mask ) #NEWLINE# #UNINDENT# return tuple ( batch_with_masks )