How to extract and print a weight matrix from a ckpt file?

462 views
Skip to first unread message

Jui-Yi Tsai

unread,
Dec 10, 2016, 12:20:08 PM12/10/16
to Discuss
I tried to execute the attached code "word2vec_optimized.py," which is edited from the original one cloned from the github of TensorFlow.

The lines 437 to 458 in the attached code are shown as follows:

    f_voc = open('vocab.txt', 'r')
    lines = f_voc.readlines()
    rev_dict = []
    for x in lines:
    rev_dict.append(x.strip())
    f_voc.close()
    final_embeddings = np.zeros((71291, 200)) 
    restored_model = model.saver.restore(session,'model.ckpt-2265170')
    #print_tensors_in_checkpoint_file("model.ckpt-2265157", "w_out")
    fout = open('output_word2vec_test.txt', 'w')  

    for i in range(len(final_embeddings)):
    for j in range(len(final_embeddings[i])):
    if j == 0:
    fout.write(str(rev_dict[i]) + ' ' + str(final_embeddings[i][j]) + ' ')
    elif j == (len(final_embeddings[i])-1):
fout.write(str(final_embeddings[i][j]) + '\n')
    else:
fout.write(str(final_embeddings[i][j]) + ' ')
    fout.close()
    print(restored_model)

With the above lines, I tried to print the words and their corresponding vectors like the following format, which looks like an array:

apple 0.123456 -1.234567 ... 0.987654
banana 0.123456 -1.234567 ... 0.987654
cat 0.123456 -1.234567 ... 0.987654


But I can't manage to extract a matrix in a ckpt file after checking it with "inspect_checkpoint.py".....How should I do? Is there any suggestion? Thanks.
word2vec_optimized.py

Yuxin Wu

unread,
Dec 10, 2016, 6:40:31 PM12/10/16
to Discuss
You can use tf.train.NewCheckpointReader, which provides method to read the values directly.
Reply all
Reply to author
Forward
0 new messages