当我在小型语料库上运行此代码时,我收到密钥错误,因为该密钥不在 dict 中,而且语料库词汇也不是那么大。
sim = similarity.eval()
for i in xrange(valid_size):
valid_word = reverse_dictionary[valid_examples[i]]
print("--",valid_word)
top_k = 5 # number of nearest neighbors
nearest = (-sim[i, :]).argsort()[1:top_k+1]
print(nearest)
log_str = "Nearest to %s:" % valid_word
print(log_str)
for k in xrange(top_k):
close_word = reverse_dictionary[nearest[k]]
我的输出是这样的:
Average loss at step 0 : 139.830688477
[[ 0.01613899 -0.06088334 -0.043384 ..., 0.02021606 -0.10094199
0.16063547]
[ 1.00000012 0.10277888 -0.20193034 ..., -0.04780241 0.07802841
0.13258868]
[ 0.09824251 -0.17075592 0.10143445 ..., 0.09903113 -0.08740355
-0.00371696]
...,
[-0.01591019 0.02056946 0.09188825 ..., -0.0506176 0.07684846
0.06354721]
[-0.06749535 0.0028128 -0.09138335 ..., 0.09473826 0.04847325
-0.00853895]
[ 0.01795161 0.01850585 0.04632751 ..., 0.11854959 0.11196665
-0.00684015]]
16
[-0.01613899 0.06088334 0.043384 ..., -0.02021606 0.10094199
-0.16063547]
<type 'numpy.ndarray'>
[ 31 113 118 ..., 650 353 233]
-- using
[113 118 555 298 150]
Nearest to using:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-129-cf006e08ddb8> in <module>()
87 for k in xrange(top_k):
88
---> 89 close_word = reverse_dictionary[nearest[k]]
90 log_str = "%s %s," % (log_str, close_word)
91 print(log_str)
KeyError: 555
词汇长度 = 1155
批量大小 = 16
嵌入尺寸 = 128
跳过窗口 = 5
num_skips = 4
有效大小 = 16
有效窗口 = 100
valid_examples = np.random.choice(valid_window, valid_size, replace=False)
num_sampled =64
有人可以帮忙吗?
嗨,拉胡尔,
我遇到了与您完全相同的问题,直到我在错误发生之前使用“print(len(reverse_dictionary))”检查了“reverse_dictionary”数组的大小。
您的“vocabulary_size = 50000”行应该设置得更低。 我将它设置为通过打印“reverse_dictionary”的长度返回的值,不再有任何问题。
我希望这有帮助。
这有很大帮助!!!!!! @tcheightyeight
由于最近没有活动而自动关闭。 当有更多信息可用时,请重新打开。
最有用的评论
嗨,拉胡尔,
我遇到了与您完全相同的问题,直到我在错误发生之前使用“print(len(reverse_dictionary))”检查了“reverse_dictionary”数组的大小。
您的“vocabulary_size = 50000”行应该设置得更低。 我将它设置为通过打印“reverse_dictionary”的长度返回的值,不再有任何问题。
我希望这有帮助。