我们在本地导出的模型可以正常工作,但在Google Cloud ML中创建新模型版本时失败,具体如下:
创建版本失败。模型验证失败:输出的外部维度必须未知,'Const_2:0'的外部维度为1 有关如何导出Tensorflow SavedModel的更多信息,请参见https://www.tensorflow.org/api_docs/python/tf/saved_model.
我们当前导出的模型响应在tensorflow-serve
和gcloud predict local
中可以正常工作,响应如下:
outputs { key: "categories" value { dtype: DT_STRING tensor_shape { dim { size: 1 } dim { size: 17 } } string_val: "Business Essentials" string_val: "Business Skills" string_val: "Communication" string_val: "Customer Service" string_val: "Desktop Computing" string_val: "Finance" string_val: "Health & Wellness" string_val: "Human Resources" string_val: "Information Technology" string_val: "Leadership" string_val: "Management" string_val: "Marketing & Advertising" string_val: "Personal Development" string_val: "Project Management" string_val: "Sales" string_val: "Technical Skills" string_val: "Training & Development" }}outputs { key: "category" value { dtype: DT_STRING tensor_shape { dim { size: 1 } } string_val: "Training & Development" }}outputs { key: "class" value { dtype: DT_INT64 tensor_shape { dim { size: 1 } } int64_val: 16 }}outputs { key: "prob" value { dtype: DT_FLOAT tensor_shape { dim { size: 1 } dim { size: 17 } } float_val: 0.051308773458 float_val: 2.39087748923e-05 float_val: 4.77133402232e-11 float_val: 0.00015225057723 float_val: 0.201782479882 float_val: 2.11781745287e-17 float_val: 3.61836161034e-09 float_val: 0.104659214616 float_val: 6.55719213682e-06 float_val: 1.16744895001e-12 float_val: 1.68323947491e-06 float_val: 0.00510392058641 float_val: 3.46840134738e-12 float_val: 1.02085353504e-08 float_val: 0.000151587591972 float_val: 3.04983092289e-25 float_val: 0.636809647083 }}
问题一定出在categories上,因为其他所有输出在第一个工作版本的输出中已经存在。
有什么想法吗?
回答:
回答我自己的问题:
我需要使用现有的张量来创建一个形状为[?, len(CATEGORIES)]
的张量。
为此,我们需要一个形状为[?]
的张量,如tf.argmax(logits, 1)
,用于在categories_tensor
上使用tf.tile
,以及一个形状为[?, len(CATEGORIES)]
的张量,用于在该结果上使用tf.reshape
。所以
CATEGORIES # => ['dog', 'elephant']n_classes = len(CATEGORIES) # => 2categories_tensor = tf.constant(CATEGORIES) # => Shape [2]pob_tensor = tf.nn.softmax(logits) # => Shape [?, 2] 其中?为预测输入的数量class_tensor = tf.argmax(logits, 1) # => Shape [?, 1]tiled_categories_tensor = tf.tile(categories_tensor, tf.shape(class_tensor)) # => Shape [2*?] # => ['dog', 'elephant', 'dog', 'elephant', ... (?次) , 'dog', 'elephant' ]categories = tf.reshape(tiled_categories_tensor, tf.shape(prob_tensor)) # => Shape [?, 2] # => [['dog', 'elephant'], ['dog', 'elephant'], ... (?次) , ['dog', 'elephant'] ]predictions_dict = { 'category': tf.gather(CATEGORIES, tf.argmax(logits, 1)), 'class': class_tensor, 'prob': prob_tensor, 'categories': categories }
希望这对遇到此问题的人有所帮助