Views
No views yet
1import numpy as np
2import tensorflow as tf
3from huggingface_hub import hf_hub_download
4
5# Download the model
6hf_hub_download(repo_id="AiresPucrs/english-embedding-vocabulary-16",
7 filename="english_embedding_vocabulary_16.keras",
8 local_dir="./",
9 repo_type="model"
10 )
11
12# Download the embedding vocabulary txt file
13hf_hub_download(repo_id="AiresPucrs/english-embedding-vocabulary-16",
14 filename="english_embedding_vocabulary.txt",
15 local_dir="./",
16 repo_type="model"
17 )
18
19model = tf.keras.models.load_model('english_embedding_vocabulary_16.keras')
20
21# Compile the model
22model.compile(loss='binary_crossentropy',
23 optimizer='adam',
24 metrics=['accuracy'])
25
26with open('english_embedding_vocabulary.txt', encoding='utf-8') as fp:
27 english_embedding_vocabulary = [line.strip() for line in fp]
28 fp.close()
29
30embeddings = model.get_layer('embedding').get_weights()[0]
31
32words_embeddings = {}
33
34# iterating through the elements of list
35for i, word in enumerate(english_embedding_vocabulary):
36 # here we skip the embedding/token 0 (""), because is just the PAD token.
37 if i == 0:
38 continue
39 words_embeddings[word] = embeddings[i]
40
41print("Embeddings Dimensions: ", np.array(list(words_embeddings.values())).shape)
42print("Vocabulary Size: ", len(words_embeddings.keys()))
43