In [54]:
import tensorflow as tf
from tensorflow.contrib.framework.python.framework import checkpoint_utils
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.preprocessing import normalize
import numpy as np
In [20]:
MODEL_DIR_OCCUPATION = 'gs://conversationai-models/tf_trainer_runs/fprost/tf_gru_attention_multiclass_biosbias_glove/20190328_103117/model_dir/model.ckpt-100000'
MODEL_DIR_GENDER = 'gs://conversationai-models/tf_trainer_runs/fprost/tf_gru_attention_multiclass_warmstart_biosbias_glove/20190404_151521/model_dir/model.ckpt-191000'
In [23]:
var_list = checkpoint_utils.list_variables(MODEL_DIR_OCCUPATION)
for v in var_list:
print(v)
In [99]:
kernel_occupation = np.transpose(checkpoint_utils.load_variable(MODEL_DIR_OCCUPATION, 'dense_2/kernel'))
In [100]:
var_list = checkpoint_utils.list_variables(MODEL_DIR_GENDER)
for v in var_list:
print(v)
In [101]:
kernel_gender = np.transpose(checkpoint_utils.load_variable(MODEL_DIR_GENDER, 'final_layer/kernel'))
In [102]:
TITLE_LABELS = [
'accountant', 'acupuncturist', 'architect', 'attorney', 'chiropractor', 'comedian', 'composer', 'dentist',
'dietitian', 'dj', 'filmmaker', 'interior_designer', 'journalist', 'landscape_architect', 'magician',
'massage_therapist', 'model', 'nurse', 'painter', 'paralegal', 'pastor', 'personal_trainer',
'photographer', 'physician', 'poet', 'professor', 'psychologist', 'rapper',
'real_estate_broker', 'software_engineer', 'surgeon', 'teacher', 'yoga_teacher']
In [103]:
kernel_gender.shape
Out[103]:
In [104]:
kernel_gender_female = normalize(kernel_gender[0].reshape(1, -1))
kernel_gender_male = normalize(kernel_gender[1].reshape(1, -1))
In [105]:
kernel_gender_male
Out[105]:
In [106]:
kernel_gender_mean = normalize((kernel_gender_female + kernel_gender_male) / 2)
direction_male = kernel_gender_male - np.sum(np.multiply(kernel_gender_male, kernel_gender_mean))* kernel_gender_mean
direction_female = kernel_gender_female - np.sum(np.multiply(kernel_gender_female, kernel_gender_mean))* kernel_gender_mean
In [107]:
cosine_similarity(direction_female, direction_male)
Out[107]:
In [114]:
# Compute mean
kernel_occupation_mean = np.mean(kernel_occupation, axis=0)
# Apply x - np.sum(np.multiply(x, mean))* mean
In [119]:
for i in range(33):
# _bias = np.abs(cosine_similarity(kernel_gender_female, kernel_occupation[i].reshape(1, -1))) + \
# np.abs(cosine_similarity(kernel_gender_male, kernel_occupation[i].reshape(1, -1)))
_bias = (cosine_similarity(kernel_gender_female - kernel_gender_male, kernel_occupation[i].reshape(1, -1)))
# _bias = cosine_similarity(direction_male, kernel_occupation[i].reshape(1, -1))
# x = kernel_occupation[i].reshape(1, -1)
# x = x - np.sum(np.multiply(x, kernel_occupation_mean))* kernel_occupation_mean
# _bias = cosine_similarity(direction_male, x)
print ('{}:{}'.format(TITLE_LABELS[i], float(_bias)))
In [ ]: