Skip to content

Instantly share code, notes, and snippets.

@youben11
Created September 28, 2021 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youben11/5e6f224ea983b4b98d61c3f2494118bf to your computer and use it in GitHub Desktop.
Save youben11/5e6f224ea983b4b98d61c3f2494118bf to your computer and use it in GitHub Desktop.
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