[英]TensorFlow SavedModel output output had no dimensions when loading a model with BigQuery ML
我正在尝试使用 BigQuery ML 加载已保存的 tensorflow model 来进行预测。 但是,当我运行读取 GCS 中保存的 model 的查询时,出现以下错误:
TensorFlow SavedModel output output had no dimensions, which is not supported
我使用 tf.compat.v1.estimator.experimental.KMeans 保存了一个 K-Means tf.compat.v1.estimator.experimental.KMeans
,这是我的代码:
def input_fn():
return tf.data.Dataset.from_tensors(tf.convert_to_tensor(df.loc[:,col_features], dtype=tf.float32)).repeat(1)
## Function for convergence of kmeans
def centroids_distance(num_clusters: int, centroids_old: np.array, centroids_new: np.array) -> float:
return np.max([np.linalg.norm(centroids_old[r,:] - centroids_new[r,:]) for r in range(num_clusters)])
feature_columns = [tf.feature_column.numeric_column(feature_name, dtype=tf.float32) for feature_name in col_features]
previous_centers = np.zeros((k, len(col_features)))
kmeans = tf.compat.v1.estimator.experimental.KMeans(num_clusters=k,
seed=seed,
initial_clusters='kmeans_plus_plus',
distance_metric='squared_euclidean',
use_mini_batch=False,
feature_columns=feature_columns)
for s in range(max_iterations):
kmeans.train(input_fn)
cluster_centers = kmeans.cluster_centers()
centroids_diff = centroids_distance(k_opt, previous_centers, cluster_centers)
if centroids_diff < tol:
break
previous_centers = cluster_centers
inputFn = tf.estimator.export.build_parsing_serving_input_receiver_fn(tf.feature_column.make_parse_example_spec(feature_columns))
model_path = kmeans.export_saved_model("gs://my_bucket/my_folder/model", inputFn)
我对 Tensorflow 很陌生,我不明白错误指向什么。 我检查了export_save_model
是否有其他参数来设置 output 维度,但似乎没有一个是我想要的。 提前致谢。
在这里找到同样的问题: Is there a way to use a kmeans, tensorflow saved model in bigquery? .
主要问题是 TF 内置 KMeans 估计器 model 的 output 张量形状在已保存的 model 中具有未知等级。
解决此问题的两种可能方法:
尝试直接在 BQML 上训练 KMeans model。 重新实现 TF KMeans 估计器 model 以将 output 张量重塑为特定的张量形状。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.