나는 단어 매입 및 Tensorflow에 익숙하지 않습니다. 저는 건강 데이터에 word2vec을 적용해야하는 프로젝트를 진행하고 있습니다.
Tensorflow 웹 사이트 코드 (word2vec_basic.py
)를 사용했습니다.word2vec_basic not working (Tensorflow)
num_steps = 100001
with tf.Session(graph=graph) as session:
# We must initialize all variables before we use them.
tf.initialize_all_variables().run()
print('Initialized')
average_loss = 0
for step in range(num_steps):
batch_data, batch_labels = generate_batch(
batch_size, num_skips, skip_window)
feed_dict = {train_dataset : batch_data, train_labels : batch_labels}
_, l = session.run([optimizer, loss], feed_dict=feed_dict)
average_loss += l
if step % 2000 == 0:
if step > 0:
average_loss = average_loss/2000
# The average loss is an estimate of the loss over the last 2000 batches.
print('Average loss at step %d: %f' % (step, average_loss))
average_loss = 0
# note that this is expensive (~20% slowdown if computed every 500 steps)
if step % 10000 == 0:
sim = similarity.eval()
for i in range(valid_size):
valid_word = reverse_dictionary[valid_examples[i]]
top_k = 8 # number of nearest neighbors
nearest = (-sim[i, :]).argsort()[1:top_k+1]
log = 'Nearest to %s:' % valid_word
for k in range(top_k):
close_word = reverse_dictionary[nearest[k]]
log = '%s %s,' % (log, close_word)
print(log)
final_embeddings = normalized_embeddings.eval()<code>
이 코드는 정확히 내가 생각하지 않는 예와 동일하다 : 나는 대신 "text8.zip"의 내 데이터를 읽고 마지막 단계까지 정상적으로 실행하기 위해 조금이 코드를 수정 그건 잘못 됐어. 내가 입력 데이터의 크기를 변경
KeyError Traceback (most recent call last)
<ipython-input-20-fc4c5c915fc6> in <module>()
34 for k in xrange(top_k):
35 print(nearest[k])
---> 36 close_word = reverse_dictionary[nearest[k]]
37 log_str = "%s %s," % (log_str, close_word)
38 print(log_str)
KeyError: 2868
하지만 여전히 같은 오류를 제공합니다 : 그것은 준 오류입니다.
누군가가 나에게이 문제를 해결하는 방법에 대한 조언을 해줄 수 있다면 정말 감사 할 것입니다.
reverse_dictionary에는 2868 번째 요소가 없습니다. 가장 가까운 [k]는 사전 단어 중에서 선택해야하기 때문에 조금 이상합니다. 나는 전체 소스 코드를 더 잘 업로드하는 것이 좋다고 생각한다. 그렇지 않으면 사전 크기 및 valid_examples를 확인하십시오. – Jin
진 감사합니다. 어디서 잘못되었는지 알았지 만 해결 방법을 모릅니다. 나는 정확하게 웹 사이트의 "word2vec_basic"예제로 사용했습니다. 그러나 "text8.zip"단계에서이 파일의 일부 줄을 삭제하여 원본 텍스트로 길이가 2/5가되도록했습니다. 오류가 나타났습니다. 그리고 당신 말이 맞습니다. 제 사전에는 2045 가지 요소 만 있습니다. Skip-diagram 모델 작업을위한 최소 길이는 2868입니까? – ngoduyvu
나는 그렇게 생각하지 않는다. 단어 id 2868은 사전에서 임의로 선택됩니다.2045보다 커서는 안됩니다. 따라서 소스 코드 전체를 업로드하는 것이 좋습니다. 뭔가 틀렸어 야합니다. 그렇 겠지. – Jin