【发布时间】:2020-05-27 04:42:11
【问题描述】:
我知道这有点愚蠢,因为 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 的剩余代码)获取代码并尝试以某种方式重塑输出。在这个过程中,我意识到结果的对象,实际上是一个带有一些不同 Tensor 类的元组,似乎是用来单独构建图的。有趣的是,如果我接受这个元组并执行以下操作:
model_predictions[0][0]...[0]
对象总是一些奇怪的张量。我在三个点中上升到 60 左右,最终放弃了。
从那里我尝试获取将这些输出提供给 KmeansClustering 的类,称为集群操作中的 Kmeans(以及 github 中的周边代码)。同样,我在更改数据类型方面没有成功,但我确实理解了为什么将输出的名称设置为 Squeeze something:在这里,输出有一个挤压操作。我认为这可能是问题所在,并试图删除挤压操作...我失败了:(
最后我意识到这个输出似乎实际上来自 estimator.py 文件,此时我只是放弃了它。
感谢所有评论的人,我不会走这么远, 干杯
【问题讨论】:
标签: python tensorflow google-bigquery k-means