亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在java中提供TensorFlow - 在一個會話運行中進行多個預測

在java中提供TensorFlow - 在一個會話運行中進行多個預測

aluckdog 2022-08-03 10:51:29
我有一個保存的模型,我設法加載,運行并獲得1行9個特征的預測。(輸入)現在我試圖預測100行這樣的行,但是當嘗試從Tensor.copyTo()讀取結果數組時,我得到了不兼容的形狀java.lang.IllegalArgumentException: cannot copy Tensor with shape [1, 1] into object with shape [100, 1]顯然,我設法在循環中運行了這個預測 - 但這比一次運行100的等效python執行慢20倍。這里是 /saved_model_cli.py 報告的已保存模型信息MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:signature_def['serving_default']:  The given SavedModel SignatureDef contains the following input(s):    inputs['input'] tensor_info:        dtype: DT_FLOAT        shape: (-1, 9)        name: dense_1_input:0  The given SavedModel SignatureDef contains the following output(s):    outputs['output'] tensor_info:        dtype: DT_FLOAT        shape: (-1, 1)        name: dense_4/BiasAdd:0  Method name is: tensorflow/serving/predict問題是 - 我是否需要為我想預測的每一行運行(),就像這里的問題一樣
查看完整描述

1 回答

?
catspeake

TA貢獻1111條經驗 獲得超0個贊

好吧,所以我發現了一個問題,我無法為我想要的所有行(預測)運行一次??赡苁且粋€張量流新手問題,我搞砸了輸入和輸出矩陣。當報告工具(python)說你有一個形狀(-1,9)的輸入張量映射到java long[]{1,9}時,這并不意味著你不能傳遞long[]{1000,9}的輸入張量 - 這意味著1000行用于預測。在此輸入之后,定義為 [1,1] 的輸出張量可以是 [1000,1]。


這個代碼實際上比python運行得快得多(1.2秒對7秒),這是代碼(也許會解釋得更好)


public Tensor prepareData(){

    Random r = new Random();

    float[]inputArr = new float[NUMBER_OF_KEWORDS*NUMBER_OF_FIELDS];

    for (int i=0;i<NUMBER_OF_KEWORDS * NUMBER_OF_FIELDS;i++){

        inputArr[i] = r.nextFloat();

    }


    FloatBuffer inputBuff = FloatBuffer.wrap(inputArr, 0, NUMBER_OF_KEWORDS*NUMBER_OF_FIELDS);

    return Tensor.create(new long[]{NUMBER_OF_KEWORDS,NUMBER_OF_FIELDS}, inputBuff);

}


public void predict (Tensor inputTensor){

    try ( Session s = savedModelBundle.session()) {

        Tensor result;

        long globalStart = System.nanoTime();

            result = s.runner().feed("dense_1_input", inputTensor).fetch("dense_4/BiasAdd").run().get(0);


            final long[] rshape = result.shape();

            if (result.numDimensions() != 2 || rshape[0] <= NUMBER_OF_KEWORDS) {

                throw new RuntimeException(

                        String.format(

                                "Expected model to produce a [N,1] shaped tensor where N is the number of labels, instead it produced one with shape %s",

                                Arrays.toString(rshape)));

            }



        float[][] resultArray = (float[][]) result.copyTo(new float[NUMBER_OF_KEWORDS][1]);

        System.out.println(String.format("Total of %d,  took : %.4f ms", NUMBER_OF_KEWORDS, ((double) System.nanoTime() - globalStart) / 1000000));

        for (int i=0;i<10;i++){

            System.out.println(resultArray[i][0]);

        }

    }

}


查看完整回答
反對 回復 2022-08-03
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號