代码之家  ›  专栏  ›  技术社区  ›  Steven

高朗张量流中带嵌入层的开敞式路缘石模型

  •  6
  • Steven  · 技术社区  · 6 年前

    tfgo 包裹。该模型包括两个规则输入和两个Keras嵌入层。看起来是这样的:

    embedding_layer = Embedding(vocab_size,
                                100,
                                weights=[embedding_matrix],
                                input_length=100,
                                trainable=False)
    
    sequence_input = Input(shape=(max_length,), dtype='int32')
    embedded_sequences = embedding_layer(sequence_input)
    text_lstm = Bidirectional(LSTM(256))(embedded_sequences)
    text_lstm = Dropout(0.5)(text_lstm)
    text_lstm  = Dense(512, activation='relu')(text_lstm )
    text_lstm = Dropout(0.5)(text_lstm)
    text_lstm  = Dense(256, activation='relu')(text_lstm)
    text_lstm = Dropout(0.5)(text_lstm)
    text_lstm  = Dense(128, activation='relu')(text_lstm)
    text_lstm = Dropout(0.5)(text_lstm)
    
    title_input = Input(shape=(max_title_length,), dtype='int32')
    title_embed = Embedding(vocab_size, embedding_vector_length, input_length=max_title_length)(title_input)
    title_lstm = Bidirectional(LSTM(128))(title_embed)
    title_lstm = Dropout(0.5)(title_lstm)
    title_lstm  = Dense(512, activation='relu')(title_lstm )
    title_lstm = Dropout(0.5)(title_lstm)
    title_lstm  = Dense(256, activation='relu')(title_lstm)
    title_lstm = Dropout(0.5)(title_lstm)
    title_lstm  = Dense(128, activation='relu')(title_lstm)
    title_lstm = Dropout(0.5)(title_lstm)
    
    
    merged = concatenate([text_lstm, title_lstm]) 
    
    merged_d1 = Dense(1024, activation='relu')(merged)
    merged_d1 = Dropout(0.5)(merged_d1)
    merged_d1 = Dense(512, activation='relu')(merged_d1)
    merged_d1 = Dropout(0.5)(merged_d1)
    
    
    text_class = Dense(num_classes, activation='sigmoid')(merged_d1)
    model = Model([sequence_input, title_input], text_class)
    

    我正在尝试在Go中加载模型,到目前为止,我认为我已经能够包括如下常规输入层:

    s := make([]int32, 100)
    s1 := make([]int32, 15)
    model := tg.LoadModel("myModel3", []string{"myTag"}, nil)
    tensor1, _ := tf.NewTensor(s)
    tensor2, _ := tf.NewTensor(s1)
    
    result := model.Exec([]tf.Output{
        model.Op("dense_18/Sigmoid", 0),
    }, map[tf.Output]*tf.Tensor{
        model.Op("input_1", 0): tensor1,
        model.Op("input_3", 0): tensor2,
    })
    

    但当我运行代码时,它提醒我实际上还有两个“输入”:

    panic: You must feed a value for placeholder tensor 'input_4' with dtype int32 and shape [?,15]
         [[Node: input_4 = Placeholder[_output_shapes=[[?,15]], dtype=DT_INT32, shape=[?,15], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
    

    我尝试了以下方法:

        s := make([]int32, 100)
    s1 := make([]int32, 15)
    
    tensor1e, _ := tf.NewTensor([1][100][2]float32{})
    tensor2e, _ := tf.NewTensor([1][15][2]float32{})
    
    tensor1, _ := tf.NewTensor(s)
    tensor2, _ := tf.NewTensor(s1)
    
    result := model.Exec([]tf.Output{
        model.Op("dense_18/Sigmoid", 0),
    }, map[tf.Output]*tf.Tensor{
        model.Op("input_3", 0):                tensor1,
        model.Op("embedding_2/embeddings", 0): tensor2e,
        model.Op("embedding_1/embeddings", 0): tensor1e,
        model.Op("input_4", 0):                tensor2,
    })
    
    But this 
    

    产生了以下错误:

    2018-08-17 19:50:00.543771: W tensorflow/core/framework
    
    /op_kernel.cc:1275] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 2. But input(1) is a vector of size 3
    2018-08-17 19:50:00.543792: W tensorflow/core/framework/op_kernel.cc:1275] OP_REQUIRES failed at reduction_ops_common.h:155 : Invalid argument: Invalid reduction dimension (2 for input with 2 dimension(s)
    panic: Invalid reduction dimension (2 for input with 2 dimension(s)
         [[Node: bidirectional_4/Sum = Sum[T=DT_FLOAT, Tidx=DT_INT32, _output_shapes=[[?]], keep_dims=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](bidirectional_4/zeros_like, bidirectional_3/Sum/reduction_indices)]]
    

    有人能告诉我怎么完成这个手术的正确方向吗?任何帮助都将不胜感激!

    1 回复  |  直到 6 年前
        1
  •  3
  •   Steven    6 年前

    所以,我不需要指定嵌入层的输入。实际上,我的输入结构不正确。应该是这样的:

    tensor1, _ := tf.NewTensor([][]int32{tokes_text})
    tensor2, _ := tf.NewTensor([][]int32{tokes_title})
    
    
    result := model.Exec([]tf.Output{
                model.Op("dense_18/Sigmoid", 0),
            }, map[tf.Output]*tf.Tensor{
                model.Op("input_3", 0): tensor1,
                model.Op("input_4", 0): tensor2,
            })