aymericdamien / TensorFlow-Examples

TensorFlow Tutorial and Examples for Beginners (support TF v1 & v2)
Other
43.38k stars 14.95k forks source link

KMeans Error at "TensorFlow-Examples/examples/2_BasicModels/kmeans.py" #202

Open ghost opened 6 years ago

ghost commented 6 years ago

Hi my name is Sukkyu.

First of all, I would like to thank you for providing your code to the public. They've really been helpful.

Now I was working on Kmeans example at "examples/2_BasicModels/kmeans.py" with my Python version 3.6 and tensorflow 1.4.

It was okay until I opened the tf.session, and suddenly InvalidArgumentError occurred when I ran the main for-loop

The error message says something like "InvalidArgumentError: Invalid reduction dimension (1 for input with 1 dimension(s) ..."

I'm suspecting it has something to do with input dimension and feed_dict argument, but never figured out the solution myself. I also visited tensorflow official API page hoping to find some help but to no avail.

I'd be grateful if you could fix this.

Finally I'm attaching the full error messages below:

InvalidArgumentError                      Traceback (most recent call last)
/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1322     try:
-> 1323       return fn(*args)
   1324     except errors.OpError as e:

/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1301                                    feed_dict, fetch_list, target_list,
-> 1302                                    status, run_metadata)
   1303 

/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    472             compat.as_text(c_api.TF_Message(self.status.status)),
--> 473             c_api.TF_GetCode(self.status.status))
    474     # Delete the underlying status object from memory otherwise it stays alive

InvalidArgumentError: Invalid reduction dimension (1 for input with 1 dimension(s)
     [[Node: l2_normalize_16/Sum = Sum[T=DT_FLOAT, Tidx=DT_INT32, keep_dims=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](l2_normalize_16/Square, l2_normalize_16/Sum/reduction_indices)]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-21-85065e872a9b> in <module>()
      9 for i in range(1, num_steps + 1):
     10     _, d, idx = sess.run([train_op, avg_distance, cluster_idx],
---> 11                          feed_dict={X: full_data_x})
     12     if i % 10 == 0 or i == 1:
     13         print("Step %i, Avg Distance: %f" % (i, d))

/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    887     try:
    888       result = self._run(None, fetches, feed_dict, options_ptr,
--> 889                          run_metadata_ptr)
    890       if run_metadata:
    891         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1118     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1119       results = self._do_run(handle, final_targets, final_fetches,
-> 1120                              feed_dict_tensor, options, run_metadata)
   1121     else:
   1122       results = []

/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1315     if handle is None:
   1316       return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1317                            options, run_metadata)
   1318     else:
   1319       return self._do_call(_prun_fn, self._session, handle, feeds, fetches)

/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1334         except KeyError:
   1335           pass
-> 1336       raise type(e)(node_def, op, message)
   1337 
   1338   def _extend_graph(self):

InvalidArgumentError: Invalid reduction dimension (1 for input with 1 dimension(s)
     [[Node: l2_normalize_16/Sum = Sum[T=DT_FLOAT, Tidx=DT_INT32, keep_dims=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](l2_normalize_16/Square, l2_normalize_16/Sum/reduction_indices)]]

Caused by op 'l2_normalize_16/Sum', defined at:
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-19-33cf6fb27af4>", line 2, in <module>
    (all_scores, cluster_idx, scores, cluster_centers_initialized, init_op, train_op, _) = kmeans.training_graph()
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/contrib/factorization/python/ops/clustering_ops.py", line 367, in training_graph
    cluster_centers = nn_impl.l2_normalize(cluster_centers, dim=1)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_impl.py", line 295, in l2_normalize
    square_sum = math_ops.reduce_sum(math_ops.square(x), dim, keep_dims=True)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 1307, in reduce_sum
    name=name)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 4682, in _sum
    keep_dims=keep_dims, name=name)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
    op_def=op_def)
  File "/usr/local/sook_cluster/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Invalid reduction dimension (1 for input with 1 dimension(s)
     [[Node: l2_normalize_16/Sum = Sum[T=DT_FLOAT, Tidx=DT_INT32, keep_dims=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](l2_normalize_16/Square, l2_normalize_16/Sum/reduction_indices)]]
CassOnMars commented 6 years ago

I too had trouble running the KMeans example with TF 1.4. I submitted a pull request for the change that got it working for me. It may work for you, although I tested this on Python 2.7. https://github.com/aymericdamien/TensorFlow-Examples/pull/203

jsmith commented 6 years ago

I was having this same issue. It was caused by the way I was initializing the graph. Make sure you run the tf.global_variables_initializer() first and then run the init_op from the training_graph method!