galeone / tfgo

Tensorflow + Go, the gopher way
https://pgaleone.eu/tensorflow/go/2017/05/29/understanding-tensorflow-using-go/
Apache License 2.0
2.43k stars 155 forks source link

Error: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at lookup_table_op.cc:929 : FAILED_PRECONDITION: Table not initialized. #66

Closed PratyushGoel closed 2 years ago

PratyushGoel commented 2 years ago

Hi ! I am trying to use tfgo to compute embeddings using universal sentence encoder but facing some error. Not sure if there is some issue in the go code or something else. It seems that the model is loaded successfully. Any help is appreciated

I have downloaded the USE from https://tfhub.dev/google/universal-sentence-encoder/3

Error i am getting is:

model loaded successfully
2022-02-25 17:36:56.393949: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at lookup_table_op.cc:929 : FAILED_PRECONDITION: Table not initialized.
panic: {{function_node __inference_pruned_6740}} {{function_node __inference_pruned_6740}} Table not initialized.
         [[{{node text_preprocessor/hash_table_Lookup/hash_table_Lookup/LookupTableFindV2}}]]
         [[StatefulPartitionedCall/StatefulPartitionedCall]]

goroutine 1 [running]:
github.com/galeone/tfgo.(*Model).Exec(0xc00019c030, 0xc00023fe58, 0x1, 0x1, 0xc00023fe78, 0x0, 0x0, 0x0)
        /Users/pratyushgoel/go/pkg/mod/github.com/galeone/tfgo@v0.0.0-20210914072119-caabdf830ee3/model.go:73 +0xd7
main.main()
        /Users/pratyushgoel/workspace/experiments/tfgo-example/main.go:14 +0x2e5

Process finished with the exit code 2

On running the command saved_model_cli show --all --dir universal-sentence-encoder_3/ I get the results

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: serving_default_inputs:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 512)
        name: StatefulPartitionedCall:0
  Method name is: tensorflow/serving/predict

Concrete Functions:
  Function Name: '__call__'
    Option #1
      Callable with:
        Argument #1
          inputs: TensorSpec(shape=<unknown>, dtype=tf.string, name='inputs')

  Function Name: 'init_op'

  Function Name: 'predict'
    Option #1
      Callable with:
        Argument #1
          inputs

GO file looks like this:

package main

import (
    "fmt"
    tf "github.com/galeone/tensorflow/tensorflow/go"
    tg "github.com/galeone/tfgo"
)

func main() {
    model := tg.LoadModel("universal-sentence-encoder_3/", []string{"serve"}, nil)
    fmt.Println("model loaded successfully")

    fakeInput, _ := tf.NewTensor([1]string{"Please compute embedding for this string"})
    results := model.Exec([]tf.Output{
        model.Op("StatefulPartitionedCall", 0),
    }, map[tf.Output]*tf.Tensor{
        model.Op("serving_default_inputs", 0): fakeInput,
    })

    predictions := results[0]
    fmt.Println(predictions.Value())
}
PratyushGoel commented 2 years ago

There is some issue with how this model is saved. I tried USE 4 which doesn't throw this error