Tensorflow seq2seq解码器问题?

时间:2017-09-27 19:17:45

标签: python-3.x tensorflow translation

我尝试使用tensorflow tf.contrib.seq2seq包编写seq2seq解码器。 我想知道我的代码是否正确以及是否有更好的方法来重写它。文档不易阅读。

或者我的问题是:我怎样才能轻松调试这种代码?如何在tensorflow中检查一些中间结果?

class Decoder:
    def __init__(self, embedding, hidden_size, num_layers=1, max_length=15):
        self.embedding = embedding
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.cell = tf.nn.rnn_cell.GRUCell(hidden_size)  
        self.linear = tf.Variable(tf.random_normal(shape=(self.hidden_size, cn_total_words))*0.1)


    def __call__(self, inputs, state, encoder_outputs, encoder_state, decoder_length, mode="train"):


        with tf.variable_scope("decoder") as scope:

            inputs = tf.nn.embedding_lookup(self.embedding, inputs)
            encoder_state = tf.tile(tf.expand_dims(encoder_state, 1), (1, tf.shape(inputs)[1], 1))

            attention_mechanism = tf.contrib.seq2seq.LuongAttention(self.hidden_size, encoder_outputs)
            attn_cell = tf.contrib.seq2seq.AttentionWrapper(self.cell, attention_mechanism, self.hidden_size)
            if mode == "train":
                helper = tf.contrib.seq2seq.TrainingHelper(inputs=inputs, sequence_length=decoder_length)
            elif mode == "infer":
                helper = tf.contrib.seq2seq.GreedyEmbeddingHelper(embedding=self.embedding, 
                                                start_tokens=tf.tile([en_dict["BOS"]], [tf.shape(inputs)[0]]), end_token=en_dict["EOS"])

            decoder = tf.contrib.seq2seq.BasicDecoder(cell=attn_cell, helper=helper, 
                                                      initial_state=attn_cell.zero_state(tf.shape(inputs)[0], tf.float32))

            outputs, _, _ = tf.contrib.seq2seq.dynamic_decode(decoder=decoder)
            outputs = tf.concat([tf.expand_dims(out, 1) for out in outputs], 1)

            outputs = tf.tensordot(outputs, self.linear, axes=[[2], [0]])
            return outputs, state

运行代码时出现以下错误

  

----------------------------------------------- ---------------------------- ValueError Traceback(最近一次调用   持续)   〜/ anaconda3 / ENVS / py36 / lib中/ python3.6 /站点包/ tensorflow /蟒蛇/框架/ op_def_library.py   在apply_op中(self,op_type_name,name,** keywords)       434 preferred_dtype = default_dtype,    - > 435 as_ref = input_arg.is_ref)       436如果input_arg.number_attr和len(

     

〜/ anaconda3 / ENVS / py36 / LIB / python3.6 /站点包/ tensorflow /蟒/框架/ ops.py   在internal_convert_n_to_tensor中(values,dtype,name,as_ref,   preferred_dtype)       736 as_ref = as_ref,    - > 737 preferred_dtype = preferred_dtype))       738返回ret

     

〜/ anaconda3 / ENVS / py36 / LIB / python3.6 /站点包/ tensorflow /蟒/框架/ ops.py   在internal_convert_to_tensor中(value,dtype,name,as_ref,   preferred_dtype)       如果ret为None,则为675:    - > 676 ret = conversion_func(value,dtype = dtype,name = name,as_ref = as_ref)       677

     

〜/ anaconda3 / ENVS / py36 / LIB / python3.6 /站点包/ tensorflow /蟒/框架/ ops.py   在_TensorTensorConversionFunction(t,dtype,name,as_ref)中       548" Tensor转换为Tensor请求dtype%s,dtype为%s:%r"    - > 549%(dtype.name,t.dtype.name,str(t)))       550返回t

     

ValueError:Tensor转换为Tensor请求dtype float32   dtype int32:' Tensor(" seq2seq-train / decoder / ExpandDims_2:0",shape =(?,   1,?),dtype = int32)'

     

在处理上述异常期间,发生了另一个异常:

     

TypeError Traceback(最近一次调用   最后)in()         4 emb_en = np.random.uniform(low = -0.1,high = 0.1,size =(en_total_words,hidden_​​size))         5 emb_cn = np.random.uniform(low = -0.1,high = 0.1,size =(cn_total_words,hidden_​​size))   ----> 6 model = Seq2Seq(hidden_​​size,num_layers,emb_en,emb_cn)         7 sess = tf.Session()         8 init = tf.global_variables_initializer()

      init 中的

(self,hidden_​​size,   num_layers,embed_words_en,embed_words_cn)        81 encoder_outputs,encoder_state = self.encoder(self.encoder_inputs,self.encoder_length)        82 decoder_length = tf.cast(tf.reduce_sum(self.decoder_mask,1),tf.int32)   ---> 83 decoder_outputs,decoder_state = self.decoder(self.decoder_inputs,encoder_state,encoder_outputs,   encoder_state,decoder_length)        84        85#decoder_outputs.append(decoder_out)

     致电

(自我,输入,状态,   encoder_outputs,encoder_state,decoder_length,mode)        50        51个输出,_,_ = tf.contrib.seq2seq.dynamic_decode(decoder = decoder)   ---> 52输出= tf.concat(输出中输出的[tf.expand_dims(out,1)],1)        53        54输出= tf.tensordot(输出,自我线性,轴= [[2],[0]])

     

〜/ anaconda3 / ENVS / py36 / LIB / python3.6 /站点包/ tensorflow /蟒/操作/ array_ops.py   在concat(值,轴,名称)1064返回   gen_array_ops._concat_v2(values = values,1065
  轴=轴线    - > 1066 name = name)1067 1068

     

〜/ anaconda3 / ENVS / py36 / LIB / python3.6 /站点包/ tensorflow /蟒/操作/ gen_array_ops.py   在_concat_v2中(值,轴,名称)       491"""       492 result = _op_def_lib.apply_op(" ConcatV2",values = values,axis = axis,    - > 493 name = name)       494返回结果       495

     

〜/ anaconda3 / ENVS / py36 / LIB / python3.6 /站点包/ tensorflow /蟒/框架/ op_def_library.py   在apply_op中(self,op_type_name,name,** keywords)       461(前缀,dtype.name))       462否则:    - > 463引发TypeError("%s,它们都不匹配。"%前缀)       464否则:       465引发TypeError("%s无效。"%前缀)

     

TypeError:列表中的张量传递给'值' ' ConcatV2'操作有   类型[float32,int32]不匹配。

0 个答案:

没有答案