Google Cloud ML:输出的外部尺寸必须未知 [英] Google Cloud ML: Outer dimension for outputs must be unknown

查看:43
本文介绍了Google Cloud ML:输出的外部尺寸必须未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在本地获得了一个有效的导出模型,该模型正在 Google Cloud ML 中创建新模型版本,如下所示:

we got a working exported model in local that is falling to create a new model version in Google Cloud ML as follows:

Create Version failed. Model validation failed: Outer dimension for outputs must be unknown, outer dimension of 'Const_2:0' is 1 For more information on how to export Tensorflow SavedModel, seehttps://www.tensorflow.org/api_docs/python/tf/saved_model.

我们当前的导出模型响应正在 tensorflow-serve 中工作,并且 gcloud预测本地具有以下响应:

Our current exported model response is working in tensorflow-serve and gcloud predict local with this responses:

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
  }
}

问题必须归为类别,因为所有其他输出都已经在输出的第一个有效版本中了.

The issue must be in categories as all the other outputs were there already in the first working version of the output.

任何想法?

推荐答案

回答我自己的问题:

我需要使用现有形状的张量之一,我需要基于它们创建一个 [?, len(CATEGORIES)] 张量.

I need to use one of the existing tensors of the shape I need to create a [?, len(CATEGORIES)] tensor based on them.

为此,我们需要一个张量 [?] 作为 tf.argmax(logits,1),以便在categories_tensor 和张量 [?, len(CATEGORIES)] 用于在结果上使用 tf.reshape.所以

For that purpose we need a tensor [?] as tf.argmax(logits, 1) for using tf.till over the categories_tensor and a tensor [?, len(CATEGORIES)] for using tf.reshape over the result of that. So

CATEGORIES # => ['dog', 'elephant']
n_classes = len(CATEGORIES) # => 2
categories_tensor = tf.constant(CATEGORIES) # => Shape [2]
pob_tensor = tf.nn.softmax(logits) 
# => Shape [?, 2] being ? the number of inputs to predict
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', ... (? times) , 'dog', 'elephant' ]
categories = tf.reshape(tiled_categories_tensor, tf.shape(prob_tensor)) # => Shape [?, 2] 
# => [['dog', 'elephant'], ['dog', 'elephant'], ... (? times) , ['dog', 'elephant'] ]

predictions_dict = {
      'category': tf.gather(CATEGORIES, tf.argmax(logits, 1)),
      'class': class_tensor,
      'prob': prob_tensor,
      'categories': categories
    }

希望它对遇到此问题的任何人都有帮助

Hope it helps to anyone facing this issue

这篇关于Google Cloud ML:输出的外部尺寸必须未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆