keras vgg16:InvalidArgumentError (see above for traceback): Matrix size-incompatible: In[0]: [32,167

我们在使用python的时候总会出现各种各样奇怪的问题,那么我将一一的记录下来。

我在利用vgg提取的特征来进行迁移学习的时候,发现模型报了一个未知的错误:

Caused by op u'dense_2/MatMul', defined at:
  File "HARRISON_feature_model.py", line 50, in 
    model.add(Dense(4096))
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/site-packages/keras/models.py", line 489, in add
    output_tensor = layer(self.outputs[0])
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/site-packages/keras/engine/topology.py", line 603, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/site-packages/keras/layers/core.py", line 843, in call
    output = K.dot(inputs, self.kernel)
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1057, in dot
    out = tf.matmul(x, y)
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1891, in matmul
    a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 2437, in _mat_mul
    name=name)
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
    op_def=op_def)
  File "/home/idc/anaconda3/envs/hashtag/lib/python2.7/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): Matrix size-incompatible: In[0]: [32,16777216], In[1]: [4096,4096]
	 [[Node: dense_2/MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dropout_1/cond/Merge, dense_2/kernel/read)]]
	 [[Node: metrics/acc/Mean/_101 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_666_metrics/acc/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
经过查资料发现,这是因为我用的是tf后端训练的模型,而现在我又用th后端来进行训练,因此报了错,要换成tf后端,只需要在代码里面加上:

from keras import backend as K
K.set_image_dim_ordering('tf')
就行了

参考资料

[1].Keras vgg16: Matrix size-incompatible: In[0]: [16,18432], In[1]: [25088,4096].
https://stackoverflow.com/questions/45043332/keras-vgg16-matrix-size-incompatible-in0-16-18432-in1-25088-4096

你可能感兴趣的:(keras)