[英]Tensor model input - nvalidArgumentError (see above for traceback): Shape in shape_and_slice spec
Hey I am trying to set an input point for the model that i have writen in tensorflow this is the code for the classification 嘿,我试图为我在tensorflow中编写的模型设置输入点,这是分类的代码
n_dim = training_features.shape[1]
x = tf.placeholder(tf.float32, [None,n_dim])
classifier = (...)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
classifier.fit(training_features, training_labels, steps=100)
accuracy_score = classifier.evaluate(testing_features, testing_labels, steps=100)["accuracy"]
print('Accuracy', accuracy_score)
pred_a = np.asarray([x])
prediction = format(list(classifier.predict(pred_a)))
prediction_result = np.array(prediction)
output = tf.convert_to_tensor(prediction_result,dtype=None,name="output", preferred_dtype=None)
and here is my building code 这是我的建筑代码
export_path_base = sys.argv[-1]
export_path = os.path.join(
compat.as_bytes(export_path_base),
compat.as_bytes(str(FLAGS.model_version)))
print('Exporting trained model to', export_path)
builder = saved_model_builder.SavedModelBuilder(export_path)
classification_inputs = utils.build_tensor_info(y)
classification_outputs_classes = utils.build_tensor_info(output)
print('classification_signature...')
classification_signature = signature_def_utils.build_signature_def(
inputs={signature_constants.CLASSIFY_INPUTS: classification_inputs},
outputs={
signature_constants.CLASSIFY_OUTPUT_CLASSES:
classification_outputs_classes
},
method_name=signature_constants.CLASSIFY_METHOD_NAME)
tensor_info_x = utils.build_tensor_info(x)
print('prediction_signature...')
prediction_signature = signature_def_utils.build_signature_def(
inputs={'input': tensor_info_x},
outputs={
'classes' : classification_outputs_classes
},
method_name=signature_constants.PREDICT_METHOD_NAME)
print('Exporting...')
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
sess, [tag_constants.SERVING],
signature_def_map={
'predict_sound':
prediction_signature,
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
classification_signature,
},
legacy_init_op=legacy_init_op)
builder.save()
print('Saved...')
I have tried manually passing dummy data before the build and that works but i am trying to have the client stub pass data into the model dynamically. 我尝试过在构建之前手动传递虚拟数据,并且可以正常工作,但是我试图让客户端存根动态地将数据传递到模型中。 When i try run that code to build i get this error
当我尝试运行该代码进行构建时,出现此错误
InvalidArgumentError (see above for traceback): Shape in shape_and_slice spec [1,280] does not match the shape stored in checkpoint: [193,280] [[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)]]
InvalidArgumentError(请参见上面的追溯):shape_and_slice规范[1,280]中的形状与存储在检查点中的形状不匹配:[193,280] [[Node:save / RestoreV2_1 = RestoreV2 [dtypes = [DT_FLOAT],_device =“ / job:localhost] / replica:0 / task:0 / cpu:0“]((_ recv_save / Const_0,保存/恢复V2_1 /张量名称,保存/恢复V2_1 / shape_and_slices)]]]
May main goal is to have x as an input and output return the results, the output works but cant get the input to work. 可能的主要目标是让x作为输入和输出返回结果,输出有效但无法使输入起作用。
Edit: If you just put the np.array as input without going through an input function, it will work but you also give up the chance to check the input. 编辑:如果仅将np.array用作输入而不通过输入函数,它将起作用,但是您也放弃了检查输入的机会。
Tensorflow won't check your input even if it's in the wrong shape or type or somehow corrupted but to throw such an error in the middle of session. Tensorflow不会检查您的输入,即使它的形状或类型错误或以某种方式损坏了,但会在会话过程中抛出此类错误。 Since you can run it with your test data successfully, the problem should be your actual data.
由于可以成功使用测试数据运行它,因此问题应该出在实际数据上。 Thus, it's recommended to write an input function to check your data before put it into classifier.
因此,建议在将数据放入分类器之前编写一个输入函数以检查您的数据。 Note that input function should return tf.Tensor with the shape of [x,1] (x is the number of your features) instead of an np.array.
请注意,输入函数应返回[x,1]形状的tf.Tensor(x是要素的数量),而不是np.array。
Please refer to https://www.tensorflow.org/get_started/input_fn to see how to write your own input function and pass it to the classifier. 请参阅https://www.tensorflow.org/get_started/input_fn以了解如何编写自己的输入函数并将其传递给分类器。
An example of input function: 输入函数的示例:
def input_fn_predict(): # returns x, None
#do your check here or you can just print it out
feature_tensor = tf.constant(pred_a,shape=[1,pred_a.size])
return feature_tensor,None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.