首页 > 解决方案 > Tensorflow,使用 unique_with_counts 函数时出错

问题描述

我在 tensorflow 中实现了一个 K 最近邻算法,我收到以下错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: No OpKernel was registered to support Op'UniqueWithCounts' used by node UniqueWithCounts (defined at knn.py:92) with these attrs: [T=DT_BOOL, out_idx=DT_INT32]

跟踪表明错误发生在以下行中:

Caused by op 'UniqueWithCounts', defined at:
  File "knn.py", line 92, in <module>
    find_u_labels, find_idex, find_counts = tf.unique_with_counts(find_labels_k_closest_tr_products)

这是代码的相关部分:

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.5, random_state=0)
print(X_train.shape)
print(X_test.shape)
x = tf.placeholder(float, shape=X_train.shape)
y = tf.placeholder(float, shape=X_test.shape[1:])
computeL0Dist = tf.count_nonzero(x - y, axis=[1])
find_k_closest_tr_products = tf.contrib.framework.argsort(computeL0Dist, direction='ASCENDING')
find_labels_k_closest_tr_products = tf.gather(y_train, find_k_closest_tr_products[0:paramk])
print('SHAPE', find_labels_k_closest_tr_products.shape)
find_u_labels, find_idex, find_counts = tf.unique_with_counts(find_labels_k_closest_tr_products)
find_predicted_label = tf.gather(find_u_labels, tf.argmax(find_counts))

标签: python-3.xtensorflow

解决方案


如果您查看原始错误消息,它会告诉您您正在将一个类型的张量传递tf.booltf.unique_with_counts()操作:

通过具有以下属性的节点 UniqueWithCounts(在 knn.py:92 中定义):[ T=DT_BOOL , out_idx=DT_INT32]

此操作不支持布尔值。查看您的代码,我怀疑这不是您想要的?


推荐阅读