2016-04-11 11 views
3

tf.nn.embedding_lookup(word_embedding_matrix, combine_result)を呼び出す前に、テンソルフローを使用してテキストマッチングを行っています.2文からいくつかの単語を結合する必要があります。文S2からの言葉、そして「combine_result」として一緒にそれらを結合)が、コードがtf.nn.embedding_lookup(word_embedding_matrix, combine_result)に御所とき、それは私にエラーを与える:ValueError:テンソルAはテンソルBと同じグラフでなければならない

ValueError: Tensor("Reshape_7:0", shape=(1, 6), dtype=int32) must be from the same graph as Tensor("word_embedding_matrix:0", shape=(26320, 50), dtype=float32_ref).

コードはのように怒鳴るです:

import tensorflow as tf 
import numpy as np 
import os 
import time 
import datetime 
import data_helpers 

NUM_CLASS = 2 
SEQUENCE_LENGTH = 47 


    # Placeholders for input, output and dropout 
    input_x = tf.placeholder(tf.int32, [None, 2, SEQUENCE_LENGTH], name="input_x") 
    input_y = tf.placeholder(tf.float32, [None, NUM_CLASS], name="input_y") 
    dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob") 

    def n_grams(text, window_size): 
       text_left_window = [] 
       # text_left_window = tf.convert_to_tensor(text_left_window, dtype=tf.int32) 
       for z in range(SEQUENCE_LENGTH-2): 
        text_left = tf.slice(text, [z], [window_size]) 
        text_left_window = tf.concat(0, [text_left_window, text_left]) 
       text_left_window = tf.reshape(text_left_window, [-1, window_size]) 
       return text_left_window 


      def inference(vocab_size, embedding_size, batch_size, slide_window_size, conv_window_size): 
       # # Embedding layer 
       word_embedding_matrix = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), 
                name="word_embedding_matrix") 
       # convo_unit = tf.Variable(tf.random_uniform([slide_window_size*2, ], -1.0, 1.0), name="convo_unit") 

       text_comp_result = [] 
       for x in range(batch_size): 
        # input_x_slice_reshape = [[1 1 1...] 
        #       [2 2 2...]] 
        input_x_slice = tf.slice(input_x, [x, 0, 0], [1, 2, SEQUENCE_LENGTH]) 
        input_x_slice_reshape = tf.reshape(input_x_slice, [2, SEQUENCE_LENGTH]) 

        # text_left_flat: [294, 6, 2, 6, 2, 57, 2, 57, 147, 57, 147, 5, 147, 5, 2,...], length = SEQUENCE_LENGTH 
        # text_right_flat: [17, 2, 2325, 2, 2325, 5366, 2325, 5366, 81, 5366, 81, 1238,...] 
        text_left = tf.slice(input_x_slice_reshape, [0, 0], [1, SEQUENCE_LENGTH]) 
        text_left_flat = tf.reshape(text_left, [-1]) 
        text_right = tf.slice(input_x_slice_reshape, [1, 0], [1, SEQUENCE_LENGTH]) 
        text_right_flat = tf.reshape(text_right, [-1]) 

        # extract both text. 
        # text_left_window: [[294, 6, 2], [6, 2, 57], [2, 57, 147], [57, 147, 5], [147, 5, 2],...] 
        # text_right_window: [[17, 2, 2325], [2, 2325, 5366], [2325, 5366, 81], [5366, 81, 1238],...] 
        text_left_window = n_grams(text_left_flat, slide_window_size) 
        text_right_window = n_grams(text_right_flat, slide_window_size) 
        text_left_window_sha = text_left_window.get_shape() 
        print 'text_left_window_sha:', text_left_window_sha 

        # composite the slice 
        text_comp_list = [] 
        # text_comp_list = tf.convert_to_tensor(text_comp_list, dtype=tf.float32) 
        for l in range(SEQUENCE_LENGTH-slide_window_size+1): 
         text_left_slice = tf.slice(text_left_window, [l, 0], [1, slide_window_size]) 
         text_left_slice_flat = tf.reshape(text_left_slice, [-1]) 
         for r in range(SEQUENCE_LENGTH-slide_window_size+1): 
          text_right_slice = tf.slice(text_right_window, [r, 0], [1, slide_window_size]) 
          text_right_slice_flat = tf.reshape(text_right_slice, [-1]) 

          # convo_unit = [294, 6, 2, 17, 2, 2325] 
          convo_unit = tf.concat(0, [text_left_slice_flat, text_right_slice_flat]) 
          convo_unit_reshape = tf.reshape(convo_unit, [-1, slide_window_size*2]) 
          # convo_unit_shape_val = convo_unit_reshape.get_shape() 
          # print 'convo_unit_shape_val:', convo_unit_shape_val 

          embedded_chars = tf.nn.embedding_lookup(word_embedding_matrix, convo_unit_reshape) 
          embedded_chars_expanded = tf.expand_dims(embedded_chars, -1) 
     ... 

を喜ばせることができ誰か助けて?どうもありがとうございました!

+3

コードが不完全です(グラフやセッションの初期化位置がわかりません)が、新しいデフォルトグラフ。計算の前に "tf.reset_default_graph()"を実行し、それ以上グラフを作成しないでください(すなわち、tf.Graphへの呼び出し) –

+1

ありがとうございました!私はこの問題を "tf.reset_default_graph()"を追加した後に修正しました。 –

答えて

0

ヤロスラフは、上記のコメントに答えた - 答えに移動:

を使用すると、新しいデフォルトのグラフを作成するときに、このエラーが発生します。計算の前にtf.reset_default_graph()を実行し、それ以上のグラフを作成しないでください(つまり、tf.Graphへの呼び出し)

関連する問題