擅长:python、mysql、java
<p>如果要使用<code>.tsv</code>在TensorBoard中标记单词嵌入,请使用以下代码片段。它使用<a href="https://docs.python.org/3/library/csv.html" rel="nofollow noreferrer">CSV</a>模块(请参见<a href="https://stackoverflow.com/a/29896136/6853900">Doug's answer</a>)。</p>
<pre><code># /bin/env python3
import csv
def save_vocabulary():
label_file = "word2context/labels.tsv"
with open(label_file, 'w', encoding='utf8', newline='') as tsv_file:
tsv_writer = csv.writer(tsv_file, delimiter='\t', lineterminator='\n')
tsv_writer.writerow(["Word", "Count"])
for word, count in word_count:
tsv_writer.writerow([word, count])
</code></pre>
<p><code>word_count</code>是这样的元组列表:</p>
<pre><code>[('the', 222594), ('to', 61479), ('in', 52540), ('of', 48064) ... ]
</code></pre>