Вопрос TF должен быть решен - PullRequest
0 голосов
/ 17 февраля 2020

Здравствуйте, ниже является частью моего кода

private ListView stockListView;
private String stringUrl;
private ListAdapter listAdapter;
private Context context;
private String stockName[];
static String urlData=null;
private TensorFlowInferenceInterface inferenceInterface;
static float Prediction=0;
static List predictHigh=new ArrayList();
private String inputName;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    float[] FloatPredict=predict(0);
    Prediction=FloatPredict[0];
}

//Method go over here
private float[] predict(int index){
    // model has only 1 output neuron
    float output[] = new float[1];
    // feed network with input of shape (1,input.length) = (1,2)
    //int result= Integer.parseInt((String) predictHigh.get(index));
    float preOutput[]=new float[1];
    Log.d("Tensorflow", String.valueOf(preOutput[0]));
    for(int i=0;i<predictHigh.size();i++){
        preOutput[i]= (float) predictHigh.get(i);
    }
    inferenceInterface.feed("StockInput",preOutput);
    inferenceInterface.run(new String[]{"StockOutput"});
    inferenceInterface.fetch("StockOutput", output);

// return prediction
    return output;
}

ошибка:

E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.example.stockreprediction, PID: 23984
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stockreprediction/com.example.stockreprediction.MainActivity}:
 java.lang.IllegalArgumentException: No Operation named [StockInput] in
 the Graph
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
         at android.app.ActivityThread.access$800(ActivityThread.java:151)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:135)
         at android.app.ActivityThread.main(ActivityThread.java:5254)
         at java.lang.reflect.Method.invoke(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:372)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
      Caused by: java.lang.IllegalArgumentException: No Operation named [StockInput] in the Graph
         at org.tensorflow.Session$Runner.operationByName(Session.java:372)
         at org.tensorflow.Session$Runner.feed(Session.java:142)
         at org.tensorflow.contrib.android.TensorFlowInferenceInterface.addFeed(TensorFlowInferenceInterface.java:577)
         at org.tensorflow.contrib.android.TensorFlowInferenceInterface.feed(TensorFlowInferenceInterface.java:318)
         at com.example.stockreprediction.MainActivity.predict(MainActivity.java:214)
         at com.example.stockreprediction.MainActivity.onCreate(MainActivity.java:101)
         at android.app.Activity.performCreate(Activity.java:5990)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
         at android.app.ActivityThread.access$800(ActivityThread.java:151) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:135) 
         at android.app.ActivityThread.main(ActivityThread.java:5254) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:372) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 I/Process: Sending signal. PID: 23984 SIG: 9 Process 23984 terminated.

Моя модель из Кераса использует этот код:

def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
    """
    Freezes the state of a session into a pruned computation graph.

    Creates a new computation graph where variable nodes are replaced by
    constants taking their current value in the session. The new graph will be
    pruned so subgraphs that are not necessary to compute the requested
    outputs are removed.
    @param session The TensorFlow session to be frozen.
    @param keep_var_names A list of variable names that should not be frozen,
                      or None to freeze all the variables in the graph.
    @param output_names Names of the relevant graph outputs.
    @param clear_devices Remove the device directives from the graph for better portability.
    @return The frozen graph definition.
    """
    graph = session.graph
    with graph.as_default():
        freeze_var_names = list(set(v.op.name for v in tf.global_variables())
        .difference(keep_var_names or []))
        output_names = output_names or []
        output_names += [v.op.name for v in tf.global_variables()]
        input_graph_def = graph.as_graph_def()
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""
        frozen_graph = tf.graph_util.convert_variables_to_constants(
            session, input_graph_def, output_names, freeze_var_names)
        return frozen_graph


//end of above

from keras import backend as K
import tensorflow as tf
# Create, compile and train model...

frozen_graph = freeze_session(K.get_session(),output_names=[out.op.name for out in model.outputs])

И я проверьте, что сводка моей модели находится здесь:

Слой (тип) Выходная форма Параметр #

StockInput (InputLayer) (Нет, 30, 1) 0


bidirectional_3 (Двунаправленное (Нет, 1500) 4512000


StockOutput (Плотный) (Нет, 1) 1501

Всего параметров: 4,513,501 Обучаемых параметров: 4,513,501 Необучаемых параметров: 0


Я не знаю, почему android говорят, что не могут найти StockInput на графике `

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...