Sunday, August 26, 2018

Opening Keras model with embedding layer in Tensorflow in Golang

Leave a Comment

I am trying to implement my Keras neural network in Go using the tfgo package. The model includes 2 regular inputs and two Keras embedding layers. It looks like this:

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) 

I'm trying to load the model in Go, so far I think I've been able to include the regular input layers like this:

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

But when I run the code, it reminds me that there are actually two more "inputs":

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"]()]] 

I would imagine that these would be "input2" and "input4" and that they would need to be initialized somehow with the embeddings from the model, but I have no idea how I could do this in Tensorflow in Go (I am new to Go).

I tried the following:

    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  

produced the following, error:

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)]] 

Can anyone point me in the right direction on how to complete this operation? Any help would be much appreciated!

1 Answers

Answers 1

So, it turns out that I did not need to specify the inputs for the embedding layers. I was actually structuring the input incorrectly. It should look like this:

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,         }) 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment