2017-05-01 35 views
1

我一直爭取與tensorflow的製造商能夠爲我的模型,我想服務模型服tensorflow分類

我的問題是如何將我喂輸入後,將數據提供給我的分類模型? 我已經看到由谷歌創建以來教程

使用的代碼,並試圖實現它

classify_inputs_tensor_info = utils.build_tensor_info(
      serialized_tf_example) 
     classes_output_tensor_info = utils.build_tensor_info(classes) 
     scores_output_tensor_info = utils.build_tensor_info(values) 

     classification_signature = signature_def_utils.build_signature_def(
      inputs={ 
       signature_constants.CLASSIFY_INPUTS: classify_inputs_tensor_info 
      }, 
      outputs={ 
       signature_constants.CLASSIFY_OUTPUT_CLASSES: 
        classes_output_tensor_info, 
       signature_constants.CLASSIFY_OUTPUT_SCORES: 
        scores_output_tensor_info 
      }, 
      method_name=signature_constants.CLASSIFY_METHOD_NAME) 

,並從我瞭解的輸入被傳遞給顧名思義張量稱爲serialized_tf_example串行輸入字符串,但他們然後使用tf.FixedLenFeature,我不明白,然後解析serialized_tf_example與tf.parse_example並將其分配給模型內使用的x,但我想分析它到一個分類器它接受數組作爲輸入,但不知道如何去做繞過這個。

在落實這一點,我寫這

serialized_tf_example = tf.placeholder(tf.string, name='tf_example') 
     feature_configs = { 'audio/encoded': tf.FixedLenFeature(shape=[193], dtype=tf.float32, default_value=input_x),} 
     tf_example = tf.parse_example(serialized_tf_example, feature_configs) 
     x = tf_example['audio/encoded'] 

     sess = tf.InteractiveSession() 
     sess.run(tf.global_variables_initializer()) 

     # Define the dimensions in the feature columns 
     feature_columns = [tf.contrib.layers.real_valued_column("", dimension=5)] 

     classifier = tf.contrib.learn.DNNLinearCombinedClassifier(
      dnn_feature_columns=feature_columns, dnn_hidden_units=[200,300], n_classes=10, 
      dnn_optimizer=tf.train.GradientDescentOptimizer(
       learning_rate=0.01 
      ) 
     ) 

     #run training 
     classifier.fit(input_fn=get_train_inputs, steps=100) 
     #testing 
     accuracy_score = classifier.evaluate(input_fn=get_test_inputs, steps=10)["accuracy"] 
     print('Test accuracy : ', format(accuracy_score)) 

     prediction = format(list(classifier.predict_classes(x, as_iterable=True))) 

而且x是一個張量,所以是不能夠被讀取。當我嘗試使用運行或.eval()它要求我饋送值serialized_tf_example

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'tf_example' with dtype string [[Node: tf_example = Placeholderdtype=DT_STRING, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

當我使用預測=格式(列表(classifier.predict_classes(np.array(x)中,as_iterable =真) ) 我得到

InvalidArgumentError (see above for traceback): Shape in shape_and_slice spec [1,200] does not match the shape stored in checkpoint: [193,200] [[Node: save/RestoreV2_1 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_1/tensor_names, save/RestoreV2_1/shape_and_slices)]]

回答

0

可以/應該使用classifier.predict沒有火車和eval返回x tf.Example.Your input_fn,Y,你可以寫一個predict_input_fn類似於其他輸入功能。

predictoin = next(classifier.predict_classes(input_fn=predict_input_fn)) 

請注意,如果使用list函數得到所有的預測結果,函數應以異常結束。你可以檢查tf.estimator.inputs.numpy_input_fn

+0

嘿@ user1454804我試圖實現輸入函數,但我得到TypeError:x必須是字典;得到張量' –