2017-04-17 31 views
0

嘿,我想設置爲我在tensorflow 這已經writen模型的輸入點是分類張量模型的輸入 - nvalidArgumentError(見上文回溯):外形在shape_and_slice規範

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_and_slice規範[1,280]存儲在 檢查點的形狀不匹配:[193280] [[節點: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)]]

5個主要目標是將x作爲輸入和輸出返回結果,輸出有效但是不能得到投入的工作。

回答

1

編輯:如果您只是將np.array作爲輸入而不經過輸入函數,它將起作用,但您也放棄檢查輸入的機會。

即使Tensorflow的形狀或類型不正確,或者以某種方式損壞,但在會話中發生此類錯誤,Tensorflow也不會檢查您的輸入。由於您可以成功運行測試數據,因此問題應該是您的實際數據。因此,建議在寫入分類器之前編寫一個輸入函數來檢查數據。請注意,輸入函數應返回tf.Tensor,形狀爲[x,1](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 
+1

這不會提供一個答案的問題。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/16060019) – IanS

+0

我想我已經明確表示錯誤是由於他將一個np.array傳遞給一個需要tf.Tensor作爲輸入的函數而引起的,回答了他的問題。 –

+0

嗨@秦海陽如果你的意思是我將一個數組傳遞給預測,那麼你的解決方案將不起作用,因爲它不接受張量作爲輸入。 –

相關問題