2017-10-06 2 views
0

TensorFlow, word2vec 및 신경 네트워크를 처음 사용하며 이에 대해 배우려고합니다. 저는이 TensorFlow 튜토리얼을 작성 중입니다 : https://www.tensorflow.org/tutorials/word2vec. 나는 여기에있는 튜토리얼을 위해 word2vec_optimized.py 코드를 실행했다 : https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec_optimized.py. 튜토리얼 코드 실행이 끝나면 저장된 TensorFlow 모델을 출력합니다. 모델을 다시로드하여 단어 비교에 사용할 수 있는지, 즉 러시아가 모스크바에있는 것처럼 프랑스가 파리에 있는지를 확인하려고합니다. 내가 튜토리얼 코드에서 볼TensorFlow에서 저장된 모델을로드하는 방법 word2vec 자습서 및 단어 비교에 사용

이 사용 될 수있는 비유의 방법이있다 :

def analogy(self, w0, w1, w2): 
    """Predict word w3 as in w0:w1 vs w2:w3.""" 
    wid = np.array([[self._word2id.get(w, 0) for w in [w0, w1, w2]]]) 
    idx = self._predict(wid) 
    for c in [self._id2word[i] for i in idx[0, :]]: 
     if c not in [w0, w1, w2]: 
     print(c) 
     break 
    print("unknown") 

하지만 먼저, 난 내 주요 방법으로 할 저장된 모델을 다시로드해야합니다 :

def main(_): 
    with tf.Graph().as_default(), tf.Session() as session: 
    with tf.device("/cpu:0"): 
     model = tf.train.import_meta_graph('model.ckpt.meta') 
     model.restore(session, tf.train.latest_checkpoint('/results/')) 
     model.analogy(b'france', b'paris', b'russia') 

이 다음과 같은 오류가 발생합니다

Traceback (most recent call last): 
    File "./word2vec_test.py", line 539, in <module> 
    tf.app.run() 
    File "/util/opt/anaconda/2.2/envs/tensorflow-1.0.0/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run 
    _sys.exit(main(_sys.argv[:1] + flags_passthrough)) 
    File "./embedding_tutorial/embedding/word2vec_test.py", line 534, in main 
    model.analogy(b'france', b'paris', b'russia') 
AttributeError: 'Saver' object has no attribute 'analogy' 

가 어떻게 저장 모듈을로드하고 사용할 수 있습니다 유추 법을 부르는거야? 내 주 방법을 유추 방법과 동일한 파일에 넣고 있습니다.

답변

0

나는 analogy()Word2Vec 클래스의 메소드라고 생각하지만, model을 해당 유형의 객체로 인스턴스화하지는 않습니다.

opts = Options() 
with tf.Graph().as_default(), tf.Session() as session: 
    with tf.device("/cpu:0"): 
    model = Word2Vec(opts, session) 
    model.saver = tf.train.import_meta_graph('/path/to/model.ckpt.meta') 
    model.saver.restore(session, 
         tf.train.latest_checkpoint('/path/to/results/')) 
    model.analogy(b'france', b'paris', b'russia') 

을 또는 당신이 훈련 할 때 --interactive을 사용하는 경우 훈련이 끝나면 실제로, 당신은 ipython으로 interactive mode로 들어갑니다 :

이보십시오.

+0

답변 해 주셔서 감사합니다. 위의 코드를 시도한 후에이 오류가 발생했습니다 : 'NotFoundError (역 추적에 대해서는 위 참조) : 체크 포인트에 키 sm_w_t가 없습니다. [노드 : 저장/복원 V2_3 = RestoreV2 [dtypes = [DT_FLOAT], _device = 전체 오류는 상당히 길지만, "/ job : localhost/replica : 0/task : 0/cpu : 0"] (recv_save/Const_0, save/RestoreV2_3/tensor_names, save/RestoreV2_3/shape_and_slices)]] 도움이된다면 나머지 추적 표시를 게시 할 수 있습니다. 또한 훈련을 위해 - 대화 형 플래그를 시도 할 것이지만, 훈련을 다시 마칠 때까지 약간의 시간이 걸릴 것입니다. –

+0

코드 스 니펫을 업데이트했습니다. 다시 해봐? --interactive가 작동하는지 확인하려면 --epochs_to_train = 1 (기본값 15 대신)을 설정하여 시간을 절약하십시오. '\t \t NotFoundError (역 추적에 대한 위 참조) : – greeness

+0

나는 새 코드와 같은 오류가 있어요 키 sm_w_t하는 체크 포인트 에서 찾을 수 없습니다 [[노드 : 저장/RestoreV2_3 = RestoreV2 [dtypes = [DT_FLOAT, _device = "/ job : localhost/replica : 0/task : 0/cpu : 0"] (_ recv_save/Const_0, 저장/복원 v2_3/tensor_names, 저장/복원 V2_3/shape_and_slices)]] 둘 이상이있을 경우 중요합니까? 결과 폴더의 모델? 모델을 두 번 이상 생성했습니다. 감사! –

관련 문제