首页 文章

在Golang中使用Tensorflow中的嵌入层打开Keras模型

提问于
浏览
6

我正在尝试使用 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中执行此操作(我是Go的新手) ) .

我尝试了以下方法:

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 回答

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

相关问题