【问题标题】:Tensor model input - nvalidArgumentError (see above for traceback): Shape in shape_and_slice spec张量模型输入 - nvalidArgumentError(参见上面的回溯):shape_and_slice 规范中的形状
【发布时间】:2017-04-17 19:30:28
【问题描述】:

嘿,我正在尝试为我在 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)

这是我的建筑代码

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...')

我曾尝试在构建之前手动传递虚拟数据,但我试图让客户端存根将数据动态传递到模型中。 当我尝试运行该代码来构建时,我得到了这个错误

InvalidArgumentError(参见上面的回溯):Shape in shape_and_slice 规范 [1,280] 与存储在 检查点:[193,280] [[节点:保存/RestoreV2_1 = 恢复V2[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)]]

可能的主要目标是让 x 作为输入并输出返回结果,输出有效但无法使输入正常工作。

【问题讨论】:

    标签: python tensorflow tensorflow-serving


    【解决方案1】:

    编辑:如果您只是将 np.array 作为输入而不通过输入函数,它会起作用,但您也会放弃检查输入的机会。

    Tensorflow 不会检查您的输入,即使它的形状或类型错误或以某种方式损坏,但会在会话中间抛出此类错误。由于您可以使用测试数据成功运行它,因此问题应该是您的实际数据。因此,建议在将数据放入分类器之前编写一个输入函数来检查您的数据。请注意,输入函数应返回形状为 [x,1] 的 tf.Tensor(x 是您的特征数)而不是 np.array。

    请参考https://www.tensorflow.org/get_started/input_fn了解如何编写自己的输入函数并将其传递给分类器。

    输入函数示例:

    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
    

    【讨论】:

    • 这并没有提供问题的答案。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker。 - From Review
    • 我想我已经明确了这个错误是因为他将一个 np.array 传递给了一个需要 tf.Tensor 作为输入的函数,这直接回答了他的问题。
    • 嘿@QinHeyang 如果您的意思是我将数组传递给预测,那么您的解决方案将不起作用,因为它不接受张量作为输入。
    • @QinHeyang 这里的关键词是你的第一个词“可能”——这表明你在写答案时并不知道这是否是答案。如果你真的想给出一个明确的答案,我建议不要使用“可能”这个词。
    • @BrianPhiri 您可以在输入函数中返回所需的张量。我在答案中添加了一个示例,该示例在您的代码中返回 pred_a。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多