Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def evaluate(sentence):
try:
embedded = encode(sentence)
except KeyError as error:
print("! the word", error, "is unknown")
return
if embedded.shape[0] > SENTENCE_LENGTH_LIMIT:
print(f"! the sentence should not contain more than {SENTENCE_LENGTH_LIMIT} tokens")
return
padded = np.zeros((SENTENCE_LENGTH_LIMIT, 300))
padded[SENTENCE_LENGTH_LIMIT - embedded.shape[0]:, :] = embedded
original = model(torch.tensor(padded.reshape((-1, 1, 300))).float()).detach().numpy()[0, 0, 0]
simulated = homomorphic_inferer.simulate(padded)[0]
start = timeit.default_timer()
actual = homomorphic_inferer.encrypt_and_run(keys, padded)[0]
end = timeit.default_timer()
if actual < 0.35:
print("- the sentence was negative", end=' ')
elif actual > 0.65:
print("+ the sentence was positive", end=' ')
else:
print("~ the sentence was neutral", end=' ')
print(
f"("
f"original: {original * 100:.2f}%, "
f"simulated: {simulated * 100:.2f}%, "
f"actual: {actual * 100:.2f}%, "
f"difference: {np.abs(original - actual) * 100:.2f}%, "
f"took: {end - start:.3f} seconds"
f")"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment