python - 有没有办法在 bigquery 中使用 kmeans、tensorflow 保存的模型?

标签 python tensorflow google-bigquery k-means

我知道这有点愚蠢,因为 BigQueryML 现在为 Kmeans 提供了良好的初始化。尽管如此,我还是被要求在 tensorflow 中训练一个模型,然后将其传递给 BigQuery 进行预测。

我保存了我的模型并且一切正常,直到我尝试将它上传到 bigquery。我收到以下错误:

TensorFlow SavedModel output output has an unsupported shape: unknown_rank: true

所以我的问题是:在 BigQuery 中使用经过 tensorflow 训练的 kmeans 算法是不可能的吗?

编辑:

创建模型:

kmeans = tf.compat.v1.estimator.experimental.KMeans(num_clusters=8, use_mini_batch = False,    initial_clusters=KMEANS_PLUS_PLUS_INIT, seed=1234567890, relative_tolerance=.001)

服务功能:

def serving():
    inputs = {}
   # for feat in df.columns:
   #     inputs[feat] = tf.placeholder(shape=[None], dtype = tf.float32)
    inputs = tf.placeholder(shape=[None,9], dtype = tf.float32)
    return tf.estimator.export.TensorServingInputReceiver(inputs,inputs)

保存模型:

kmeans.export_saved_model("gs://<bicket>/tf_clustering_model", 
                          serving_input_receiver_fn=serving,
                          checkpoint_path='/tmp/tmpdsleqpi3/model.ckpt-19',
                          experimental_mode=tf.estimator.ModeKeys.PREDICT)

加载到 BigQuery:

query="""
CREATE MODEL `<project>.<dataset>.kmeans_tensorflow` OPTIONS(MODEL_TYPE='TENSORFLOW', MODEL_PATH='gs://<bucket>/tf_clustering_model/1581439348/*')
"""
job = bq.Client().query(query)
job.result()

编辑2:

saved_model_cli 命令的输出如下:

jupyter@tensorflow-20200211-182636:~$ saved_model_cli  show --dir . --all

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['all_distances']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['input'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 9)
        name: Placeholder:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['output'] tensor_info:
        dtype: DT_FLOAT
        shape: unknown_rank
        name: add:0
  Method name is: tensorflow/serving/predict

signature_def['cluster_index']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['input'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 9)
        name: Placeholder:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['output'] tensor_info:
        dtype: DT_INT64
        shape: unknown_rank
        name: Squeeze_1:0
  Method name is: tensorflow/serving/predict

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['input'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 9)
        name: Placeholder:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['output'] tensor_info:
        dtype: DT_INT64
        shape: unknown_rank
        name: Squeeze_1:0
  Method name is: tensorflow/serving/predict

输出形状的等级似乎都未知。我如何设置这个特定估算器的导出或者是否有我可以搜索的东西来帮助我?

最终编辑:

至少在我看来,这似乎确实不受支持。我的方法各不相同,但最终,我发现自己别无选择,只能从 KmeansClustering 类的源代码(以及来自 github 的剩余代码)获取代码并尝试以某种方式 reshape 输出。在这个过程中,我意识到结果的对象,实际上是一个带有一些不同 Tensor 类的元组,似乎是用来单独构建图形的。有趣的是,如果我拿这个元组做类似的事情:

model_predictions[0][0]...[0]

对象总是一些奇怪的张量。我在三个点中达到了 60 左右,最终放弃了。

从那里我试图获得将这些输出提供给 KmeansClustering 的类,该类在集群操作中称为 Kmeans(以及 github 中的周围代码)。我再次没有成功更改数据类型,但我确实理解为什么输出的名称设置为 Squeeze something:这里的输出有一个挤压操作。我认为这可能是问题所在,并试图删除挤压操作等......我失败了:(

最后我意识到这个输出似乎实际上来自 estimator.py 文件,此时我就放弃了。

感谢所有评论的人,我不会走到这一步, 干杯

最佳答案

您可以使用 tensorflow 附带的命令行程序 saved_model_cli 检查保存的模型文件中的形状。

确保您在 tensorflow 中的导出签名指定了输出张量的形状。

关于python - 有没有办法在 bigquery 中使用 kmeans、tensorflow 保存的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60174534/

相关文章:

python - 如何使用 opencv 和 python 获取 Gstreamer 直播流?

sql - 如何在 SQL 中对 COUNT 列表求和

sql - Google Big Query SQL - 获取最新的列值

python - 无法使用唯一的单词/短语计数器 - Python

python - 试图减少 python leetcode 问题中的运行时间

tensorflow - 使用tf.tile复制张量N次

python - Tensorflow 2.0 : custom keras metric caused tf. 函数回溯警告

python - 为什么 "tf.Variable([.3], tf.float32)"在 tensorflow 中工作?

sql - BigQuery子查询基于所有外部列的返回数字

python - 如何更改服务器上的最大邮件大小限制?