运行时展平操作中的Keras重塑错误

时间:2017-03-23 13:03:27

标签: python-2.7 tensorflow theano keras

我在使用keras后端(tf或theano)时遇到问题。 当我执行我的模型时,其定义如下:

model = Sequential()
model.add(Conv1D(filters=2, kernel_size=15, padding="valid", input_shape=(training_feature_maps.shape[1], training_feature_maps.shape[2]), kernel_constraint=maxnorm(3)))
model.add(Activation('relu'))
model.add(Dropout(rate=0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(128, kernel_constraint=maxnorm(3)))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('softmax'))

print model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(training_feature_maps, y_train, batch_size=100, epochs=50, validation_data=(testing_feature_maps, y_test), shuffle=True)

输入要素贴图具有形状(~26000, 1050, 4)(~8000, 1050, 4)

摘要已打印并且看起来很好:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dropout_1 (Dropout)          (None, 1050, 4)           0         
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 1036, 2)           122       
_________________________________________________________________
activation_1 (Activation)    (None, 1036, 2)           0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 1036, 2)           0         
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 518, 2)            0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 1036)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               132736    
_________________________________________________________________
activation_2 (Activation)    (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 2)                 258       
_________________________________________________________________
activation_3 (Activation)    (None, 2)                 0         
=================================================================
Total params: 133,116.0
Trainable params: 133,116.0
Non-trainable params: 0.0
_________________________________________________________________

但是在运行期间,我使用Tensorflow作为后端得到以下错误:

Caused by op u'flatten_1/Reshape', defined at:
  File "load_data.py", line 598, in <module>
    model.add(Flatten())
  File "/home/sasse/.local/lib/python2.7/site-packages/keras/models.py", line 455, in add
    output_tensor = layer(self.outputs[0])
  File "/home/sasse/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 554, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/sasse/.local/lib/python2.7/site-packages/keras/layers/core.py", line 495, in call
    return K.batch_flatten(inputs)
  File "/home/sasse/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1744, in batch_flatten
    x = tf.reshape(x, tf.stack([-1, prod(shape(x)[1:])]))
  File "/home/sasse/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2630, in reshape
    name=name)
  File "/home/sasse/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "/home/sasse/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/sasse/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): size 1 must be non-negative, not -31193103
     [[Node: flatten_1/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](max_pooling1d_1/Squeeze, flatten_1/stack)]]
     [[Node: mul_2/_45 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_870_mul_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

当使用theano时,我收到以下错误:

RuntimeError: c_extract: Some CudaNdarray has dim 2 on broadcastable dimension 2
Apply node that caused the error: GPU_mrg_uniform{CudaNdarrayType(float32, 3D),inplace}(<CudaNdarrayType(float32, vector)>, MakeVector{dtype='int64'}.0)
Toposort index: 52
Inputs types: [CudaNdarrayType(float32, vector), TensorType(int64, vector)]
Inputs shapes: [(92160,), (3,)]
Inputs strides: [(1,), (8,)]
Inputs values: ['not shown', array([ 100, 1036,    2])]
Outputs clients: [['output'], [GpuElemwise{Composite{Cast{float32}(LT(i0, i1))}}[(0, 0)](GPU_mrg_uniform{CudaNdarrayType(float32, 3D),inplace}.1, CudaNdarrayConstant{[[[ 0.5]]]})]

我使用CUDA 8.0.44和CuDNN 5105在Titan X GPU上运行。

奇怪的是,当我在卷积中只使用一个过滤器时,一切正常。

你有什么想法,为什么会这样?

致以最诚挚的问候,

罗马

0 个答案:

没有答案