Comments (2)
I fixed it, may it be useful for someone 😄
def explain(model, sample, col_name='Title'):
""" Computes the effect each word had on model predictions """
sample = dict(sample)
sample_col_tokens = [tokens[token_to_id.get(tok, 0)] for tok in sample[col_name].split()]
data_drop_one_token = pd.DataFrame([sample] * (len(sample_col_tokens) + 1))
for drop_i in range(len(sample_col_tokens)):
data_drop_one_token.loc[drop_i, col_name] = ' '.join(UNK if i == drop_i else tok
for i, tok in enumerate(sample_col_tokens))
model.eval()
model.to('cpu')
with torch.no_grad():
*predictions_drop_one_token, baseline_pred = model(make_batch(data_drop_one_token))
diffs = baseline_pred - torch.Tensor(predictions_drop_one_token)
return list(zip(sample_col_tokens, diffs.numpy().tolist()))
from nlp_course.
Upd:
similarly also need to be corrected here
i = np.random.randint(len(data))
print("Index:", i)
model.eval()
print("Salary (gbp):", np.expm1(model(make_batch(data.iloc[i: i+1])).detach().numpy()))
tokens_and_weights = explain(model, data.loc[i], "Title")
draw_html([(tok, weight * 5) for tok, weight in tokens_and_weights], font_style='font-size:20px;');
tokens_and_weights = explain(model, data.loc[i], "FullDescription")
draw_html([(tok, weight * 10) for tok, weight in tokens_and_weights]);
from nlp_course.
Related Issues (20)
- week_2 seminar.ipynb
- week-5 readme broken link HOT 1
- solution HOT 2
- A question of bow_vocabulary in w2_homework_part1
- Please add reference for negative sampling
- Solution to homework? HOT 1
- requirement.txt HOT 1
- Week02 seminar.ipynb broken link
- 05_seminar: error - unexpected keyword argument 'input_ids'
- the classfication assignment dowload error.
- Missing word in text about residual connections
- Error in the formula from week 1 in lecture slides.
- can't download the data HOT 3
- Help with seminar on conversation HOT 2
- [question] tools for creating graphics HOT 3
- is there any discussion group? HOT 2
- download data... 403 Forbidden HOT 3
- Homework
- Download uk and ru embeddings by wget command in Google Colab (week01_embeddings/homework) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nlp_course.