创建LAC的模型
(args, vocab_size, num_labels, mode='train')
| 151 | |
| 152 | |
| 153 | def create_model(args, vocab_size, num_labels, mode='train'): |
| 154 | """创建LAC的模型""" |
| 155 | |
| 156 | # 模型输入定义 |
| 157 | words = fluid.layers.data( |
| 158 | name='words', shape=[-1, 1], dtype='int64', lod_level=1) |
| 159 | targets = fluid.layers.data( |
| 160 | name='targets', shape=[-1, 1], dtype='int64', lod_level=1) |
| 161 | |
| 162 | # 生成预测用的网络 |
| 163 | if mode == 'infer': |
| 164 | crf_decode = lex_net(words, args, vocab_size, |
| 165 | num_labels, target=None) |
| 166 | return {"feed_list": [words], |
| 167 | "words": words, |
| 168 | "crf_decode": crf_decode, } |
| 169 | |
| 170 | # 生成测试和训练用网络 |
| 171 | avg_cost, crf_decode = lex_net( |
| 172 | words, args, vocab_size, num_labels, target=targets) |
| 173 | |
| 174 | (precision, recall, f1_score, num_infer_chunks, num_label_chunks, |
| 175 | num_correct_chunks) = fluid.layers.chunk_eval( |
| 176 | input=crf_decode, |
| 177 | label=targets, |
| 178 | chunk_scheme="IOB", |
| 179 | num_chunk_types=int(math.ceil((num_labels - 1) / 2.0))) |
| 180 | chunk_evaluator = fluid.metrics.ChunkEvaluator() |
| 181 | chunk_evaluator.reset() |
| 182 | |
| 183 | ret = { |
| 184 | "feed_list": [words, targets], |
| 185 | "words": words, |
| 186 | "targets": targets, |
| 187 | "avg_cost": avg_cost, |
| 188 | "crf_decode": crf_decode, |
| 189 | "chunk_evaluator": chunk_evaluator, |
| 190 | "num_infer_chunks": num_infer_chunks, |
| 191 | "num_label_chunks": num_label_chunks, |
| 192 | "num_correct_chunks": num_correct_chunks |
| 193 | } |
| 194 | return ret |
| 195 | |
| 196 | |
| 197 | def create_pyreader(args, file_name, feed_list, place, |