Last active
November 3, 2023 21:41
-
-
Save pjoshi30/2bb20ab8ae613cde59386630b39fa897 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
MIT License | |
Copyright (c) 2023 Aimon Labs Inc. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
""" | |
import numpy as np | |
def max_neg_log_p(prob_matrix): | |
# Calculating -log(p_ij) and finding the maximum value over all tokens in each sentence | |
max_neg_log_p_values = np.max(-np.log(prob_matrix), axis=1) | |
return max_neg_log_p_values # This will be an array of shape (num_sentences,) | |
def entropy(prob_matrix): | |
prob_matrix = np.clip(prob_matrix, 1e-10, 1) | |
# Calculating the entropy for each token in each sentence | |
entropy_values = -np.sum(prob_matrix * np.log(prob_matrix), axis=1) | |
return entropy_values # This will be an array of shape (num_sentences,) | |
def max_entropy(prob_matrix): | |
# Calculating the maximum entropy over all tokens in each sentence | |
entropy_values = entropy(prob_matrix) | |
max_entropy_values = np.max(entropy_values) # This should be np.max instead of np.mean | |
return max_entropy_values # This will return a single value | |
# Assume a simplified scenario with 3 sentences and a vocabulary of 5 words | |
# Each row in prob_matrix represents a sentence, and each column represents the probability of a word in the vocabulary | |
prob_matrix = np.array([ | |
[0.7, 0.1, 0.1, 0.05, 0.05], # Sentence 1 | |
[0.2, 0.3, 0.1, 0.2, 0.2], # Sentence 2 | |
[0.1, 0.1, 0.6, 0.1, 0.1] # Sentence 3 | |
]) | |
max_entropy_value = max_entropy(prob_matrix) | |
print(f'Maximum Entropy: {max_entropy_value}') | |
max_neglogp_value = max_neg_log_p(prob_matrix) | |
print(f'Maximum Neg Log P: {max_neglogp_value}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment