WebWorker中的TensorflowJS通用句子编码器

时间:2019-09-25 03:24:35

标签: javascript tensorflow web-worker tensorflow.js

尝试在Webworker中运行USE以避免性能问题,但是它缺少embed()方法。

WebWorker

importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.2.9/dist/tf.min.js');
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder@1.1.1/dist/universal-sentence-encoder.min.js');
tf.setBackend('cpu');

onmessage = message => {
  use.load().then(model => {
    const sentences = [
      'I like my phone.',
      'Your cellphone looks great.'
    ];
    /**
      model = { model, tokenizer }
    */
    model.embed(sentences).then(async embeddings => {
      const vec = await embeddings.array();
      const cosine = tf.losses.cosineDistance(vec[0], vec[1], 0);
      const result = await cosine.data();
      console.log(`${Math.round((1 - result) * 100)}%`);
    });
  });
}

模型内部没有embed(),主线程就是这种情况
甚至可以在Webworkers中运行USE吗?

1 个答案:

答案 0 :(得分:0)

通用句子编码器(USE)可以在Webworker中使用。参见下面的工作片段,并将结果从工作程序发送到主线程

 public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level);
    Utils.transparentStatusAndNavigation(LevelActivity.this);
    layout = findViewById(R.id.layout);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }
    toolbar = findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(R.string.select_level);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });



    recyclerView.setLayoutManager(new LinearLayoutManager(LevelActivity.this));
    levelNo = MainActivity.dbHelper.GetLevelById(Constant.CATE_ID, Constant.SUB_CAT_ID);