在Golang的Tensorflow中打开具有嵌入层的Keras模型

时间:2018-08-18 00:02:57

标签: python tensorflow go keras

我正在尝试使用tfgo包在Go中实现我的Keras神经网络。该模型包括2个常规输入和2个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"]()]]

我会想象这些将是“ input2”和“ input4”,并且需要通过模型中的嵌入以某种方式对其进行初始化,但是我不知道如何在Go的Tensorflow中做到这一点(我是新手入门)。

我尝试了以下操作:

    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 个答案:

答案 0 :(得分:3)

因此,事实证明,我不需要为嵌入层指定输入。我实际上是在错误构造输入。它应该看起来像这样:

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,
        })