【问题标题】:Opening Keras model with embedding layer in Tensorflow in Golang在 Golang 的 Tensorflow 中打开带有嵌入层的 Keras 模型
【发布时间】:2018-08-18 00:02:57
【问题描述】:

我正在尝试使用 tfgo 包在 Go 中实现我的 Keras 神经网络。该模型包括 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)]]

谁能指出我如何完成此操作的正确方向?任何帮助将非常感激!

【问题讨论】:

    标签: python tensorflow go keras


    【解决方案1】:

    所以,事实证明我不需要为嵌入层指定输入。我实际上是在错误地构建输入。它应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      相关资源
      最近更新 更多