/
evaluation.py
35 lines (22 loc) · 1.12 KB
/
evaluation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import numpy as np
def evaluate(df, model_gen, n_runs=20, n_steps=20, key_words=['spicy', 'chinese', 'fish', 'easy', 'indian']):
"""Evaluates the performance of the model with a reward simulation: If key word appears then reward!"""
N_kws = len(key_words)
rewards = np.zeros((N_kws, n_runs, n_steps))
for kw, key_word in enumerate(key_words):
for run in range(n_runs):
model = model_gen()
for step in range(n_steps):
action = model.call()
if key_word in (df.loc[action.numpy(), 'summary'].lower()
+ ' '
+ df.loc[action.numpy(), 'title'].lower()
+ ' '
+ (' '.join([tag for tag in df.loc[action.numpy(), 'tags']])).lower()):
reward = 1
else:
reward = 0
rewards[kw, run, step] = reward
model.train_step((action, reward))
average_total_reward = np.mean(np.sum(rewards, axis=2))
return average_total_reward