2017-05-01 36 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必须是字典;得到张量' –