为什么我无法将张量添加到一起?

时间:2017-07-09 18:52:56

标签: python keras

我目前正在实施在this page上定义的RCL图层。

import keras
from keras.models import Model
from keras.layers import Input, Dense, Dropout, Flatten
from keras.layers import merge, Conv2D, MaxPooling2D, Input
import numpy as np
from keras import backend as K

#   RCL:
#   BatchNorm(Relu(conv(L-1) + conv(L)))
#

def make_RCNN(dim_1,dim_2,dim_3,number_of_rcl,num_of_filter, filtersize):
    return True

def RCL(feed_forward_input,num_of_filter, filtersize):
    conv = Conv2D(filters=num_of_filter, kernel_size=filtersize)
    recurrent_input = conv(feed_forward_input)
    merged = merge([feed_forward_input,recurrent_input],mode='sum')
    conv_relu = Relu(merged)
    conv_relu_batchnorm = BatchNormalization()(conv_relu)
    return conv_relu_batchnorm

input = Input(shape=(30,30,3))
output = RCL(feed_forward_input=input,num_of_filter=3,filtersize=3)

我收到错误消息:

python RCNN.py 
Using TensorFlow backend.
Couldn't import dot_parser, loading of dot files will not be possible.
RCNN.py:22: UserWarning: The `merge` function is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
  merged = merge([feed_forward_input,recurrent_input],mode='sum')
/usr/local/lib/python2.7/dist-packages/keras/legacy/layers.py:460: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
  name=name)
Traceback (most recent call last):
  File "RCNN.py", line 28, in <module>
    output = RCL(feed_forward_input=input,num_of_filter=3,filtersize=3)
  File "RCNN.py", line 22, in RCL
    merged = merge([feed_forward_input,recurrent_input],mode='sum')
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/layers.py", line 460, in merge
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/layers.py", line 111, in __init__
    node_indices, tensor_indices)
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/layers.py", line 155, in _arguments_validation
    'Layer shapes: %s' % input_shapes)
ValueError: Only layers of same output shape can be merged using sum mode. Layer shapes: [(None, 30, 30, 3), (None, 28, 28, 3)]

它说形状不一样......这确实是一个问题,只会导致对RCL的误解。

但是从我的理解,也在这里定义

RCN explanation

我实现的是一个RCL,它接受来自前一层(前馈张量)的张量,并在该张量上应用卷积(reccurent张量),这两个张量构成第一部分和第二部分z_ijk(t)方程。在此之后是两个张量计算的总和,就像根据等式,这是函数有一些问题,因为复杂的前馈输入=循环输入不具有相同的大小..所以如何总和两个张量,当它们的大小不一样的时候?

1 个答案:

答案 0 :(得分:2)

我认为解决方案可能就像在Conv2D调用中添加None参数一样简单,因此RCL方法如下所示:

padding='same'

如果没有它,Conv2D图层会将输入调整为(28,28,3)图像的大小,该图像无法与原始图像合并。 padding参数用零填充图像,因此输出具有与输入相同的形状(或相同形状的简单部分)。