R. M.
2018-12-04 11:56:41 UTC
Hi,
I've written a script to automatize the tuning of hyperparameters for a
doc2vec model. However, I wanted to check if I'm doing things right, since
my results are decent but not great.
Basically, I'm defining a number of values to test for each hyperparameter
of interest, and then the script does a doc2vec model for every combination
of these hyperparameter values on a training corpus (85% of my total
corpus). Each resulting model is tested on a testing corpus (15% of my
total corpus), by trying to guess the tag of each document (by inferring
the vector for the document, then looking at the most similar tag vector).
The overall accuracy and f1 score of each model is computed.
Here is the relevant part my code:
# Set values of interest for hyperparameters
hyperparams = {
'vector_size': [50, 200],
'min_count': [2, 10],
'epochs': [20, 50],
'window': [2, 10],
'alpha': [0.025, 0.01],
}
param_size = hyperparams['vector_size']
param_count = hyperparams['min_count']
param_epochs = hyperparams['epochs']
param_window = hyperparams['window']
param_alpha = hyperparams['alpha']
# Split data into training corpus and testing corpus
x_data = df_restricted["words"]
y_data = df_restricted["tags"]
X_train, X_test, y_train, y_test = train_test_split(x_data, y_data,
test_size=0.15, random_state=42)
# including genre tag (label) as well
train_tagged_docs = [TaggedDocument(t, [label]) for t, label in zip(X_train,
y_train)]
# Train and evaluate models for every combination of hyperparameters
for param_size, param_count, param_epochs,param_window, param_alpha in
product(param_size, param_count, param_epochs,param_window, param_alpha):
# Train model
np.random.shuffle(train_tagged_docs)
model = gensim.models.doc2vec.Doc2Vec(dm=1, vector_size=param_size,
min_count=param_count, epochs=param_epochs, workers=cores, window=
param_window, alpha=param_alpha)
model.random.seed(0)
model.build_vocab(train_tagged_docs)
model.train(train_tagged_docs, total_examples=model.corpus_count, epochs
=model.iter)
# Evaluate model
X_val = np.array([model.infer_vector(t) for t in X_test])
genre_vectors = np.array([model.docvecs[x] for x in genre_list])
sims = cosine_similarity(X_val, genre_vectors)
y_val_pred = np.array(genre_list)[sims.argmax(axis=1)]
acc = accuracy_score(y_test, y_val_pred).round(4)
score = f1_score(y_test, y_val_pred, average='weighted').round(4)
Saisissez le code ici...
Here are the results I get:
f1_score accuracy size min_count epochs window alpha
0 0.332 0.3419 50 2 20 2 0.025
1 0.1729 0.2233 50 2 20 2 0.01
2 0.3273 0.3373 50 2 20 10 0.025
3 0.1786 0.2279 50 2 20 10 0.01
4 0.3131 0.3044 50 2 50 2 0.025
5 0.2956 0.326 50 2 50 2 0.01
6 0.3243 0.3142 50 2 50 10 0.025
7 0.2942 0.3249 50 2 50 10 0.01
8 0.3483 0.3511 50 10 20 2 0.025
9 0.2477 0.2849 50 10 20 2 0.01
10 0.3413 0.3522 50 10 20 10 0.025
11 0.2425 0.2787 50 10 20 10 0.01
12 0.3211 0.307 50 10 50 2 0.025
13 0.321 0.3522 50 10 50 2 0.01
14 0.3251 0.3157 50 10 50 10 0.025
15 0.3149 0.346 50 10 50 10 0.01
16 0.3173 0.3563 200 2 20 2 0.025
17 0.1893 0.2474 200 2 20 2 0.01
18 0.3223 0.3645 200 2 20 10 0.025
19 0.1847 0.2413 200 2 20 10 0.01
20 0.3053 0.3219 200 2 50 2 0.025
21 0.2915 0.3311 200 2 50 2 0.01
22 0.3069 0.3249 200 2 50 10 0.025
23 0.2914 0.3337 200 2 50 10 0.01
24 0.3352 0.3634 200 10 20 2 0.025
25 0.2547 0.3054 200 10 20 2 0.01
26 0.3459 0.3753 200 10 20 10 0.025
27 0.2475 0.3008 200 10 20 10 0.01
28 0.3125 0.3244 200 10 50 2 0.025
29 0.3035 0.3496 200 10 50 2 0.01
30 0.315 0.326 200 10 50 10 0.025
31 0.3077 0.3516 200 10 50 10 0.01
Am I doing something wrong? The f1 score and accuracy score don't seem very
good. But bear in mind that there are around 60 different labels, so the
classifier doesn't have an easy job.
Many thanks in advance!
I've written a script to automatize the tuning of hyperparameters for a
doc2vec model. However, I wanted to check if I'm doing things right, since
my results are decent but not great.
Basically, I'm defining a number of values to test for each hyperparameter
of interest, and then the script does a doc2vec model for every combination
of these hyperparameter values on a training corpus (85% of my total
corpus). Each resulting model is tested on a testing corpus (15% of my
total corpus), by trying to guess the tag of each document (by inferring
the vector for the document, then looking at the most similar tag vector).
The overall accuracy and f1 score of each model is computed.
Here is the relevant part my code:
# Set values of interest for hyperparameters
hyperparams = {
'vector_size': [50, 200],
'min_count': [2, 10],
'epochs': [20, 50],
'window': [2, 10],
'alpha': [0.025, 0.01],
}
param_size = hyperparams['vector_size']
param_count = hyperparams['min_count']
param_epochs = hyperparams['epochs']
param_window = hyperparams['window']
param_alpha = hyperparams['alpha']
# Split data into training corpus and testing corpus
x_data = df_restricted["words"]
y_data = df_restricted["tags"]
X_train, X_test, y_train, y_test = train_test_split(x_data, y_data,
test_size=0.15, random_state=42)
# including genre tag (label) as well
train_tagged_docs = [TaggedDocument(t, [label]) for t, label in zip(X_train,
y_train)]
# Train and evaluate models for every combination of hyperparameters
for param_size, param_count, param_epochs,param_window, param_alpha in
product(param_size, param_count, param_epochs,param_window, param_alpha):
# Train model
np.random.shuffle(train_tagged_docs)
model = gensim.models.doc2vec.Doc2Vec(dm=1, vector_size=param_size,
min_count=param_count, epochs=param_epochs, workers=cores, window=
param_window, alpha=param_alpha)
model.random.seed(0)
model.build_vocab(train_tagged_docs)
model.train(train_tagged_docs, total_examples=model.corpus_count, epochs
=model.iter)
# Evaluate model
X_val = np.array([model.infer_vector(t) for t in X_test])
genre_vectors = np.array([model.docvecs[x] for x in genre_list])
sims = cosine_similarity(X_val, genre_vectors)
y_val_pred = np.array(genre_list)[sims.argmax(axis=1)]
acc = accuracy_score(y_test, y_val_pred).round(4)
score = f1_score(y_test, y_val_pred, average='weighted').round(4)
Saisissez le code ici...
Here are the results I get:
f1_score accuracy size min_count epochs window alpha
0 0.332 0.3419 50 2 20 2 0.025
1 0.1729 0.2233 50 2 20 2 0.01
2 0.3273 0.3373 50 2 20 10 0.025
3 0.1786 0.2279 50 2 20 10 0.01
4 0.3131 0.3044 50 2 50 2 0.025
5 0.2956 0.326 50 2 50 2 0.01
6 0.3243 0.3142 50 2 50 10 0.025
7 0.2942 0.3249 50 2 50 10 0.01
8 0.3483 0.3511 50 10 20 2 0.025
9 0.2477 0.2849 50 10 20 2 0.01
10 0.3413 0.3522 50 10 20 10 0.025
11 0.2425 0.2787 50 10 20 10 0.01
12 0.3211 0.307 50 10 50 2 0.025
13 0.321 0.3522 50 10 50 2 0.01
14 0.3251 0.3157 50 10 50 10 0.025
15 0.3149 0.346 50 10 50 10 0.01
16 0.3173 0.3563 200 2 20 2 0.025
17 0.1893 0.2474 200 2 20 2 0.01
18 0.3223 0.3645 200 2 20 10 0.025
19 0.1847 0.2413 200 2 20 10 0.01
20 0.3053 0.3219 200 2 50 2 0.025
21 0.2915 0.3311 200 2 50 2 0.01
22 0.3069 0.3249 200 2 50 10 0.025
23 0.2914 0.3337 200 2 50 10 0.01
24 0.3352 0.3634 200 10 20 2 0.025
25 0.2547 0.3054 200 10 20 2 0.01
26 0.3459 0.3753 200 10 20 10 0.025
27 0.2475 0.3008 200 10 20 10 0.01
28 0.3125 0.3244 200 10 50 2 0.025
29 0.3035 0.3496 200 10 50 2 0.01
30 0.315 0.326 200 10 50 10 0.025
31 0.3077 0.3516 200 10 50 10 0.01
Am I doing something wrong? The f1 score and accuracy score don't seem very
good. But bear in mind that there are around 60 different labels, so the
classifier doesn't have an easy job.
Many thanks in advance!
--
You received this message because you are subscribed to the Google Groups "Gensim" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gensim+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Gensim" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gensim+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.