def result_callback(id_to_label, output_file, res): """ When TF inference completes, assemble the results to write to disk. """ data = {} order = [id_to_label[i] for i in range(len(id_to_label))] correct, total = 0, 0 for (gold, cid), (probs, ) in res: total += 1 if gold == order[probs.flatten().argmax()]: correct += 1 data[cid] = (gold, {k: v for k, v in zip(order, probs.flatten())}) logging.info("Accuracy: %.3f", float(correct) / total) write_probabilities(data, options.output)
acc, preds, prob = sess.run( [ model.accuracy, model.predictions, model.log_probs ], feed_dict={ model.compiler.loom_input_tensor: batch_feed, model._keep_probability: 1.0 }) accs.append(acc) probs.append(prob) probs = numpy.concatenate(probs, axis=0) logging.info("Accuracy: %f", sum(accs) / len(accs)) data = {} order = [id_to_label[i] for i in range(len(id_to_label))] for dist, (lid, _, cid) in zip(probs, instances): cid = id_to_cid.get(cid, str(cid)) g = id_to_label[lid] data[cid] = (g, {k: v for k, v in zip(order, dist.flatten())}) write_probabilities(data, options.output) else: logging.error( "You must specify the input and output files, and either a training file, or testing and model files!" ) finally: shutil.rmtree(temppath)
# for (cid, label), text in zip(gold, instances): # total_probs = {k : v for k, v in prior.items()} # seq = [None for i in range(options.max_ngram)] # for c in list(text) + [None]: # seq = seq[1:] # seq.append(c) # single_probs = {} # for n in range(1, options.max_ngram + 1): # b = backoffs[n] # num_gram = tuple(seq[-n:]) # den_gram = num_gram[:-1] # for l, v in model.items(): # num = v[n].get(num_gram, 0) # #print(num_gram, den_gram, l) # if num > 0: # den = v[n - 1][den_gram] # #print(num, den) # prob = num / den # #print(prob, b) # single_probs[l] = single_probs.get(l, 0.0) + (prob * b) # for l, p in single_probs.items(): # if p != 0.0: # total_probs[l] += math.log(p) # scores[cid] = (label, total_probs) # #print(prob) # #prob += model[n][gram] * b # #model[n][gram] = model[n].get(gram, 0) + 1 write_probabilities(scores, options.output)