RuntimeError:没有为标准op:NearestNeighbors注册的C ++形状函数

时间:2017-02-15 13:10:22

标签: python python-3.x machine-learning tensorflow k-means

更新: - 请尝试以下代码: -

from tensorflow.contrib.learn.python.learn.estimators import kmeans as kmeans_lib
import random
import numpy as np
x = np.array([[random.random() for i in range(198)] for j in range(2384)])
km = kmeans_lib.KMeansClustering(num_clusters=200)
km.fit(x)

通过更改km.fit(x)行中的x类型来更改错误 例如x.astype(' float32')或x.astype(' float64')

我正在尝试使用tensorflow.contrib.learn.python.learn.estimators.kmeans实现KMeansClustering

但是在使用代码时出现以下错误: -

def cluster_data(X, num_clusters) :
    kmeans = KMeansClustering(num_clusters=num_clusters)
    kmeans.fit(X.astype('float32'))
    y = kmeans.predict(X)

"没有为标准操作注册的C ++形状函数:%s" %op.type) RuntimeError:没有为标准操作注册的C ++形状函数:NearestNeighbors"

X是一个numpy形状的数组(2000,1000)

堆栈跟踪: -

Traceback (most recent call last):
File "C:\Users\####\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-123-4bb22963e7ed>", line 1, in <module>
kmeans.fit(x.astype('float32'))
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 191, in new_func
arg_spec: Output from inspect.getargspec on the called function.
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 355, in fit
"""Initializes a BaseEstimator instance.
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 733, in _train_model
self._features_info = tensor_signature.create_signatures(features)
File "C:\Users\####\Anaconda3\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3517, in get_controller
# pylint: disable=g-doc-return-or-yield
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 733, in _train_model
self._features_info = tensor_signature.create_signatures(features)
File "C:\Users\####\Anaconda3\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2945, in device
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 699, in _train_model
'2016-09-23',
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 1052, in _get_train_ops
training hooks.
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 1021, in _call_model_fn
def __init__(self,
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\kmeans.py", line 201, in _model_fn
kmeans_plus_plus_num_retries=self.
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\factorization\python\ops\clustering_ops.py", line 295, in training_graph
# Implementation of kmeans.
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\factorization\python\ops\clustering_ops.py", line 198, in _infer_graph
with ops.colocate_with(clusters):
File "C:\Users\####\Anaconda3\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2869, in colocate_with
in the variable `scope`. This value can be used to name an
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\factorization\python\ops\clustering_ops.py", line 195, in _infer_graph
# nearest_neighbors op.
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\contrib\factorization\python\ops\gen_clustering_ops.py", line 90, in nearest_neighbors
centers=centers, k=k, name=name)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
return op
File "C:\Users\####\Anaconda3\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3517, in get_controller
# pylint: disable=g-doc-return-or-yield
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
return op
File "C:\Users\####\Anaconda3\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 4057, in name_scope
ACTIVATIONS = "activations"
File "C:\Users\####\Anaconda3\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3517, in get_controller
# pylint: disable=g-doc-return-or-yield
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 4057, in name_scope
ACTIVATIONS = "activations"
File "C:\Users\####\Anaconda3\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2763, in name_scope
c = []
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 4057, in name_scope
ACTIVATIONS = "activations"
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
return op
File "C:\Users\####\Anaconda3\lib\contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 254, in _MaybeColocateWith
if not inputs:
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 759, in apply_op
with _MaybeColocateWith(must_colocate_inputs):
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2242, in create_op
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1617, in set_shapes_for_outputs
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1568, in call_with_requiring
return getattr(x, f)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "C:\Users\####\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 680, in _call_cpp_shape_fn_impl
"No C++ shape function registered for standard op: %s" % op.type)
RuntimeError: No C++ shape function registered for standard op: NearestNeighbors

我也在ubuntu虚拟框中尝试过它但出现以下错误: -

Traceback (most recent call last):
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-0608497fa903>", line 1, in <module>
    m.fit(x)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 280, in new_func
    return func(*args, **kwargs)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 410, in fit
    SKCompat(self).fit(x, y, batch_size, steps, max_steps, monitors)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1353, in fit
    monitors=all_monitors)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 280, in new_func
    return func(*args, **kwargs)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 426, in fit
    loss = self._train_model(input_fn=input_fn, hooks=hooks)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 981, in _train_model
    config=self.config.tf_config) as mon_sess:
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 315, in MonitoredTrainingSession
    return MonitoredSession(session_creator=session_creator, hooks=all_hooks)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 601, in __init__
    session_creator, hooks, should_recover=True)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 434, in __init__
    self._sess = _RecoverableSession(self._coordinated_creator)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 767, in __init__
    _WrappedSession.__init__(self, self._create_session())
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 772, in _create_session
    return self._sess_creator.create_session()
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 494, in create_session
    self.tf_sess = self._session_creator.create_session()
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 375, in create_session
    init_fn=self._scaffold.init_fn)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/session_manager.py", line 262, in prepare_session
    sess.run(init_op, feed_dict=init_feed_dict)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _run
    feed_dict_string, options, run_metadata)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run
    target_list, options, run_metadata)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call
    raise type(e)(node_def, op, message)
InvalidArgumentError: You must feed a value for placeholder tensor 'input' with dtype float
     [[Node: input = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'input', defined at:
  File "/home/ubuntu/softwares/pycharm-community-2016.3.2/helpers/pydev/pydevconsole.py", line 526, in <module>
    pydevconsole.start_server(pydev_localhost.get_localhost(), int(port), int(client_port))
  File "/home/ubuntu/softwares/pycharm-community-2016.3.2/helpers/pydev/pydevconsole.py", line 359, in start_server
    process_exec_queue(interpreter)
  File "/home/ubuntu/softwares/pycharm-community-2016.3.2/helpers/pydev/pydevconsole.py", line 218, in process_exec_queue
    more = interpreter.add_exec(code_fragment)
  File "/home/ubuntu/softwares/pycharm-community-2016.3.2/helpers/pydev/_pydev_bundle/pydev_console_utils.py", line 251, in add_exec
    more = self.do_add_exec(code_fragment)
  File "/home/ubuntu/softwares/pycharm-community-2016.3.2/helpers/pydev/_pydev_bundle/pydev_ipython_console.py", line 41, in do_add_exec
    res = bool(self.interpreter.add_exec(codeFragment.text))
  File "/home/ubuntu/softwares/pycharm-community-2016.3.2/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 451, in add_exec
    self.ipython.run_cell(line, store_history=True)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-0608497fa903>", line 1, in <module>
    m.fit(x)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 280, in new_func
    return func(*args, **kwargs)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 410, in fit
    SKCompat(self).fit(x, y, batch_size, steps, max_steps, monitors)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1353, in fit
    monitors=all_monitors)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 280, in new_func
    return func(*args, **kwargs)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 426, in fit
    loss = self._train_model(input_fn=input_fn, hooks=hooks)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 932, in _train_model
    features, labels = input_fn()
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/learn_io/data_feeder.py", line 430, in input_builder
    self._input_dtype, 'input')
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/learn_io/data_feeder.py", line 426, in get_placeholder
    dtypes.as_dtype(dtype), [None] + shape[1:], name=name_prepend)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1520, in placeholder
    name=name)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2149, in _placeholder
    name=name)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2395, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1264, in __init__
    self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input' with dtype float
     [[Node: input = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

更新: - 根据@Changming Sun给出的建议我试图更新gen_clustering_ops.py,它解决了第一个问题,但我生成了一个新的错误。

    Traceback (most recent call last):
File "C:\Users\#####\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-21-4bb22963e7ed>", line 1, in <module>
kmeans.fit(x.astype('float32'))
File "C:\Users\#####\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 280, in new_func
return func(*args, **kwargs)
File "C:\Users\#####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 410, in fit
SKCompat(self).fit(x, y, batch_size, steps, max_steps, monitors)
File "C:\Users\#####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 1353, in fit
monitors=all_monitors)
File "C:\Users\#####\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 280, in new_func
return func(*args, **kwargs)
File "C:\Users\#####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 426, in fit
loss = self._train_model(input_fn=input_fn, hooks=hooks)
File "C:\Users\#####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 933, in _train_model
self._check_inputs(features, labels)
File "C:\Users\#####\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\estimator.py", line 731, in _check_inputs
(str(features), str(self._features_info)))
ValueError: Features are incompatible with given information. Given features: Tensor("input:0", shape=(?, 198), dtype=float32), required signatures: TensorSignature(dtype=tf.float64, shape=TensorShape([Dimension(None), Dimension(198)]), is_sparse=False).

我在张量流中面临与GMM类似的问题

ValueError: Features are incompatible with given information. Given features: Tensor("input:0", shape=(?, 198), dtype=float32)

1 个答案:

答案 0 :(得分:0)

打开 “LIB \站点包\ tensorflow \的contrib \因式分解\蟒\ OPS \ gen_clustering_ops.py”

添加

ops.RegisterShape("NearestNeighbors")(None)

对于此类错误,请以这种方式修复。