bojone / Capsule

A Capsule Implement with Pure Keras
350 stars 108 forks source link

keras 2.1.2 theano 0.9 gpu error #1

Closed jcjview closed 6 years ago

jcjview commented 6 years ago

Traceback (most recent call last):
File "capsule_test.py", line 46, in
capsule = Capsule(10, 16, 3, True)(cnn)
File "/home/.../anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 603, in call
output = self.call(inputs, *kwargs)
File "/data/.../study/Capsule/Capsule_Keras.py", line 60, in call
c = K.softmax(b)
File "/home/.../anaconda3/lib/python3.6/site-packages/keras/backend/theano_backend.py", line 1552, i n softmax
return T.nnet.softmax(x)
File "/home/.../anaconda3/lib/python3.6/site-packages/theano/tensor/nnet/nnet.py", line 809, in soft max
return softmax_op(c)
File "/home/.../anaconda3/lib/python3.6/site-packages/theano/gof/op.py", line 615, in call
node = self.make_node(
inputs, **kwargs)
File "/home/.../anaconda3/lib/python3.6/site-packages/theano/tensor/nnet/nnet.py", line 431, in make _node
x.type)
ValueError: x must be 1-d or 2-d tensor of floats. Got TensorType(float32, 3D)

I try tensorflow backend , no error.

bojone commented 6 years ago

oh, sorry. I has not any experience about theano. it seems that T.nnet.softmax only support 1-d or 2-d tensor. but keras.io do not show it.

I will try to fix it but I am sure about it.

jcjview commented 6 years ago

Thanks a lot.

bojone commented 6 years ago

now I change the code, and I build the capsule model successfully with Keras + theano, but error while training with model.fit. I has debuged long time but can not find the reason.

If you can work out, please tell me.

ymcui commented 6 years ago

@bojone Maybe it's about the wrong output shape of the K.batch_dot(), which is a Runtime Error

        for i in range(self.routings):
            c = softmax(b, 1)
            outputs = self.activation(K.batch_dot(c, u_hat_vecs, [2, 2]))   # outputs.shape = [None, num_capsule, num_capsule, dim_capsule]
            if i < self.routings - 1:
                b = K.batch_dot(outputs, u_hat_vecs, [2, 3])

the outputs's shape does not match the output of compute_output_shape.

Best, Yiming

bojone commented 6 years ago

@ymcui c.shape is [None, num_capsule, input_num_capsule] u_hat_vecs.shape = [None, num_capsule, input_num_capsule, dim_capsule] batch_dot of them has shape of [None, num_capsule, dim_capsule] why do you think of [None, num_capsule, num_capsule, dim_capsule] ?

ymcui commented 6 years ago

@bojone Maybe you can have a try with toy tensor. The result should be the shape that I have posted. Your error results from the runtime error on shape. Just a quick fix: https://github.com/ymcui/Capsule/blob/master/Capsule_Keras.py

bojone commented 6 years ago

@ymcui you mean batch_dot has a different performance with theano backend?

bojone commented 6 years ago

@ymcui you are right. I got it. thanks very much.

ymcui commented 6 years ago

You're welcome :)

wuzy361 commented 6 years ago

when I run "python capsule_test.py",there is some error as follows: Traceback (most recent call last): File "capsule_test.py", line 82, in cnn = Reshape((-1, 128))(cnn) File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.2-py2.7.egg/keras/engine/topology.py", line 554, in call output = self.call(inputs, kwargs) File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.2-py2.7.egg/keras/layers/core.py", line 402, in call target_shape = self.compute_output_shape(input_shape)[1:] File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.2-py2.7.egg/keras/layers/core.py", line 385, in compute_output_shape input_shape[1:], self.target_shape) File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.2-py2.7.egg/keras/layers/core.py", line 373, in _fix_unknown_dimension original = np.prod(input_shape, dtype=int) File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2518, in prod out=out, kwargs) File "/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py", line 35, in _prod return umr_prod(a, axis, dtype, out, keepdims) TypeError: long() argument must be a string or a number, not 'NoneType'

I update keras,but the error remains,please help me

bojone commented 6 years ago

@wuzy361 which backend?

zhangjunwang commented 6 years ago

using tensorflow backend.

when I run "python capsule_test.py",there is some error as follows: Traceback (most recent call last): File "capsule_test.py", line 83, in capsule = Capsule(10, 16, 3, True)(cnn) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 619, in call output = self.call(inputs, **kwargs) File "/home/zhangjunwang/Capsule1/Capsule_Keras.py", line 71, in call o = K.l2_normalize(o, -1) File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 3151, in l2_normalize return tf.nn.l2_normalize(x, axis=axis) TypeError: l2_normalize() got an unexpected keyword argument 'axis'

please help me!Thanks

bojone commented 6 years ago

@zhangjunwang your keras and tensorflow must be updated to lastest version. the problem is resulted from that your tensorflow version is lower than the keras needed.

zhangjunwang commented 6 years ago

@bojone Thanks,I will try it.