XifengGuo / CapsNet-Keras

A Keras implementation of CapsNet in NIPS2017 paper "Dynamic Routing Between Capsules". Now test error = 0.34%.
MIT License
2.47k stars 654 forks source link

Error about shapes not being equal in rank #34

Closed DandiC closed 6 years ago

DandiC commented 6 years ago

When I try to run the code, I get an error saying that "Shapes must be equal rank, but are 1 and 2 for 'digitcaps/map/while/MatMul' (op: 'BatchMatMul') with input shapes: [10,1152,8], [10,1152,16,8]". I am not sure why this is happening since I have not changed any part of the code, does anyone know what I'm doing wrong? Here is the full output in the console log:

Traceback (most recent call last):
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 686, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 1 and 2 for 'digitcaps/map/while/MatMul' (op: 'BatchMatMul') with input shapes: [10,1152,8], [10,1152,16,8].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/dpere013/Desktop/CapsNet-Keras-master/capsulenet.py", line 244, in <module>
    num_routing=args.routings)
  File "C:/Users/dpere013/Desktop/CapsNet-Keras-master/capsulenet.py", line 50, in CapsNet
    name='digitcaps')(primarycaps)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\topology.py", line 554, in __call__
    output = self.call(inputs, **kwargs)
  File "C:\Users\dpere013\Desktop\CapsNet-Keras-master\capsulelayers.py", line 127, in call
    inputs_hat = K.map_fn(lambda x: K.batch_dot(x, self.W, [2, 3]), elems=inputs_tiled)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 3328, in map_fn
    return tf.map_fn(fn, elems, name=name)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\functional_ops.py", line 389, in map_fn
    swap_memory=swap_memory)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2816, in while_loop
    result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2640, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2590, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\functional_ops.py", line 379, in compute
    packed_fn_values = fn(packed_values)
  File "C:\Users\dpere013\Desktop\CapsNet-Keras-master\capsulelayers.py", line 127, in <lambda>
    inputs_hat = K.map_fn(lambda x: K.batch_dot(x, self.W, [2, 3]), elems=inputs_tiled)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 915, in batch_dot
    out = tf.matmul(x, y, adjoint_a=adj_x, adjoint_b=adj_y)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1861, in matmul
    a, b, adj_x=adjoint_a, adj_y=adjoint_b, name=name)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 708, in _batch_mat_mul
    "BatchMatMul", x=x, y=y, adj_x=adj_x, adj_y=adj_y, name=name)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2958, in create_op
    set_shapes_for_outputs(ret)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2209, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2159, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 627, in call_cpp_shape_fn
    require_shape_fn)
  File "C:\Users\dpere013\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 691, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Shapes must be equal rank, but are 1 and 2 for 'digitcaps/map/while/MatMul' (op: 'BatchMatMul') with input shapes: [10,1152,8], [10,1152,16,8].
XifengGuo commented 6 years ago

@DandiC Update your keras to >=2.0.7