python-3.x - 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))
解决方案
如果您查看原始错误消息,它会告诉您您正在将一个类型的张量传递tf.bool
给tf.unique_with_counts()
操作:
通过具有以下属性的节点 UniqueWithCounts(在 knn.py:92 中定义):[ T=DT_BOOL , out_idx=DT_INT32]
此操作不支持布尔值。查看您的代码,我怀疑这不是您想要的?
推荐阅读
- javascript - 事件中的异步函数
- sql - 查询以显示异常 SQL 销售订单
- c++ - 具有与父级不同的变量名的 C++ 子结构
- tfidfvectorizer - TfidfVectorizer 仅处理第一个文档
- c# - 使用 Microsoft 消息队列 (MSMQ) 示例的 NetMQ 发布订阅
- ios - 如果用户在 IOS 中禁用了后台应用刷新,为什么不发送推送通知?
- c# - 将 XML 粘贴为类,我可以放入元数据吗
- c# - 如何对从较大的 XDocument 中提取的 XElement 执行 XPath 选择?
- javascript - 当我向选定列中的单元格添加值时,我希望它复制到另一个 Google 表格
- css - 在可变大小的框中调整图像大小并保持纵横比