简体   繁体   English

Tensor模型输入-nvalidArgumentError(请参阅上面的回溯):shape_and_slice规范中的形状

[英]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.

相关问题 InvalidArgumentError(请参见上面的回溯):重整形的输入是具有768个值的张量,但请求的形状为3072 - InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 768 values, but the requested shape has 3072 张量流中的classifier.predict错误(shape_and_slice规范中的形状不匹配) - Error for classifier.predict in tensorflow (Shape in shape_and_slice spec does not match) ResourceExhaustedError(请参见上面的回溯):分配形状为[3,3,256,512]的张量并键入float时为OOM - ResourceExhaustedError (see above for traceback): OOM when allocating tensor of shape [3,3,256,512] and type float ResourceExhaustedError(回溯见上文):分配形状为 [1,256,1024,1021] 的张量时出现 OOM - ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[1,256,1024,1021] ResourceExhaustedError(请参见上面的回溯):分配具有形状的张量时为OOM [28800,19200] - ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[28800,19200] Conv3d:InvalidArgumentError(请参阅上面的回溯):重塑的输入是张量 - Conv3d: InvalidArgumentError (see above for traceback): Input to reshape is a tensor 张量输入形状与模型拟合的问题 - Problem with tensor input shape wile fitting a model InvalidArgumentError(请参阅上面的回溯):形状[-1,150,150,1]的尺寸为负 - InvalidArgumentError (see above for traceback): Shape [-1,150,150,1] has negative dimensions 如何获取未知 PyTorch model 的输入张量形状 - How to get input tensor shape of an unknown PyTorch model Tensorflow 模型是用输入 Tensor 的形状构建的,但它在形状不兼容的输入(神经网络)上被调用 - Tensorflow model was constructed with shape for input Tensor but it was called on an input with incompatible shape (Neural Network)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM