def foreach_sentence(layer, drop_factor=<NUM_LIT:1.0>):
def sentence_fwd(docs, drop=<NUM_LIT:0.0>):<EOL><INDENT>sents = []<EOL>lengths = []<EOL>for doc in docs:<EOL><INDENT>doc_sents = [sent for sent in doc.sents if len(sent)]<EOL>subset = [<EOL>s for s in doc_sents if numpy.random.random() >= drop * drop_factor<EOL>]<EOL>if subset:<EOL><INDENT>sents.extend(subset)<EOL>lengths.append(len(subset))<EOL><DEDENT>else:<EOL><INDENT>numpy.random.shuffle(doc_sents)<EOL>sents.append(doc_sents[<NUM_LIT:0>])<EOL>lengths.append(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>flat, bp_flat = layer.begin_update(sents, drop=<NUM_LIT:0.0>)<EOL>output = layer.ops.unflatten(flat, lengths)<EOL>def sentence_bwd(d_output, sgd=None):<EOL><INDENT>d_flat = layer.ops.flatten(d_output)<EOL>d_sents = bp_flat(d_flat, sgd=sgd)<EOL>if d_sents is None:<EOL><INDENT>return d_sents<EOL><DEDENT>else:<EOL><INDENT>return layer.ops.unflatten(d_sents, lengths)<EOL><DEDENT><DEDENT>return output, sentence_bwd<EOL><DEDENT>model = wrap(sentence_fwd, layer)<EOL>return model<EOL>
Map a layer across sentences (assumes spaCy-esque .sents interface)