Download utils.py from gani2004/imagecaptioning: direct link, hf CLI and curl.
- Browser
- Download file 2.06 kB
-
https://huggingface.co/gani2004/imagecaptioning/resolve/main/utils.py
- Command line
-
hf download hf://gani2004/imagecaptioning/utils.py
-
curl -L -o utils.py https://huggingface.co/gani2004/imagecaptioning/resolve/main/utils.py
2.06 kB
| import csv | |
| import numpy as np | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| def read_glove_vecs(glove_file): | |
| with open(glove_file, 'r',encoding='utf-8') as f: | |
| words = set() | |
| word_to_vec_map = {} | |
| for line in f: | |
| line = line.strip().split() | |
| curr_word = line[0] | |
| words.add(curr_word) | |
| word_to_vec_map[curr_word] = np.array(line[1:], dtype=np.float64) | |
| i = 4 | |
| words_to_index = {} | |
| index_to_words = {} | |
| for w in sorted(words): | |
| words_to_index[w] = i | |
| index_to_words[i] = w | |
| i = i + 1 | |
| return words_to_index, index_to_words, word_to_vec_map | |
| def softmax(x): | |
| """Compute softmax values for each sets of scores in x.""" | |
| e_x = np.exp(x - np.max(x)) | |
| return e_x / e_x.sum() | |
| def convert_to_one_hot(Y, C): | |
| Y = np.eye(C)[Y.reshape(-1)] | |
| return Y | |
| def predict(X, Y, W, b, word_to_vec_map): | |
| """ | |
| Given X (sentences) and Y (emoji indices), predict emojis and compute the accuracy of your model over the given set. | |
| Arguments: | |
| X -- input data containing sentences, numpy array of shape (m, None) | |
| Y -- labels, containing index of the label emoji, numpy array of shape (m, 1) | |
| Returns: | |
| pred -- numpy array of shape (m, 1) with your predictions | |
| """ | |
| m = X.shape[0] | |
| pred = np.zeros((m, 1)) | |
| for j in range(m): # Loop over training examples | |
| # Split jth test example (sentence) into list of lower case words | |
| words = X[j].lower().split() | |
| # Average words' vectors | |
| avg = np.zeros((50,)) | |
| for w in words: | |
| avg += word_to_vec_map[w] | |
| avg = avg/len(words) | |
| # Forward propagation | |
| Z = np.dot(W, avg) + b | |
| A = softmax(Z) | |
| pred[j] = np.argmax(A) | |
| print("Accuracy: " + str(np.mean((pred[:] == Y.reshape(Y.shape[0],1)[:])))) | |
| return pred | |