Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import numpy as np
  2. from athnlp.readers.brown_pos_corpus import BrownPosTag
  3.  
  4. corpus = BrownPosTag()
  5. all_class_weights = [np.zeros(len(corpus.dictionary.x_dict)) for _ in range(len(corpus.dictionary.y_dict))]
  6.  
  7. def predict(phi, all_weights) -> int:
  8. scores = [np.dot(weights, phi) for weights in all_weights]
  9. return np.argmax(scores)
  10.  
  11. def generate_features(token_idx) -> np.array:
  12. feature_vector = np.zeros(len(corpus.dictionary.x_dict)) # [0, 0, 0, 0 ...]
  13. feature_vector[token_idx] = 1 # [0, 0, 1, 0 ...]
  14. return feature_vector
  15.  
  16. def accuracy(correct, total):
  17. return round(100*correct/total,2)
  18.  
  19. num_epochs = 3
  20. for epoch in range(num_epochs):
  21. # Shuffle the training data each epoch
  22. np.random.shuffle(corpus.train)
  23.  
  24. # Iterate through each sentence in training set and update weights
  25. for sentence in corpus.train:
  26. for token, true_label in zip(sentence.x,sentence.y):
  27. phi = generate_features(token)
  28. predicted_label = predict(phi, all_class_weights) # label index of predicted class
  29.  
  30. # Update model weights if label was predicted label index doesn't match the actual
  31. if true_label != predicted_label:
  32. all_class_weights[true_label] += phi
  33. all_class_weights[predicted_label] -= phi
  34.  
  35.  
  36. # Iterate through sentences in dev set for scoring
  37. correct_tokens, total_tokens = 0, 0
  38. for sentence in corpus.dev:
  39. for token, true_label in zip(sentence.x, sentence.y):
  40. phi = generate_features(token)
  41. predicted_label = predict(phi, all_class_weights)
  42. if predicted_label == true_label:
  43. correct_tokens += 1
  44. total_tokens += 1
  45.  
  46. print("Accuracy (%): ", accuracy(correct_tokens, total_tokens))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement