InvalidArgumentError:在[0].dim(0)和[1]中。dim(0)必须相同:[1125150]vs[32150125]

2024-09-30 16:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个合并两个源的自定义层。我收到错误“InvalidArgumentError:In[0].dim(0)和In[1].dim(0)必须相同:[1125150]vs[32150125]”。如果我将batch_size设置为1,那么[1125150]vs[1150125]将运行该代码;但是,丢失不会改变,因此仍然不是根本原因。我想我需要使用批量大小,而不是仅仅扩大尺寸

class mergeLayer(L.Layer):
    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(mergeLayer,self).__init__()
        self.kernel_initializer = INIT.get('uniform')

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(name='kernel',shape=input_shape[1:],initializer=self.kernel_initializer,trainable=True)
        super(mergeLayer,self).build(input_shape) # Be sure to call this somewhere!

    def call(self, x):
        temp = K.batch_dot(tf.expand_dims(self.kernel,0),tf.transpose(x,perm=[0,2,1]))+1
        return temp
    def compute_output_shape(self, input_shape):
        return input_shape

下面是适合模型的代码。同样,如果我在这里将batch_size更改为1,我可以运行代码,但损失保持不变。在

^{pr2}$

批处理大小为1时输出

Epoch 1/100
3903/3903 [=========================] - 45s - loss: 15.7062 - acc: 0.0254
Epoch 2/100
3903/3903 [=========================] - 43s - loss: 15.7050 - acc: 0.0254
Epoch 3/100
277/3903 [=>.......................] - ETA: 42s - loss: 15.8272 - acc: 0.0181

非常感谢您的时间和帮助。在

更新:下面是调用mergeLayer的Keras模型结构

def buildModel_merge(numClasses):
source = L.Input(shape=(64,25,1))
x = L.Conv2D(150, (3,3), activation='relu', name='conv1a')(source)
x = L.MaxPooling2D((2,2))(x)
x = L.BatchNormalization()(x)
x = L.Conv2D(150, (3,3), activation='relu', name='conv2a')(x)
x = L.Conv2D(150, (5,5), activation='relu', name='conv3a')(x)
x = L.Dropout(0.5)(x)
#reshape into a dxN matrix
x = L.Reshape((125,150))(x)
x = mergeLayer(100)(x)

source2 = L.Input(shape=(30,30,30,1))
x2 = L.Conv3D(32,(5,5,5),strides=(2,2,2),activation='relu',name='conv1b')(source2)
x2 = L.Dropout(0.2)(x2)
x2 = L.Conv3D(32,(3,3,3),activation='relu',name='conv2b')(x2)
x2 = L.MaxPooling3D(pool_size=(2,2,2),name='pool2b')(x2)
x2 = L.Dropout(0.3)(x2)
#reshape into a dxM matrix
x2 = L.Reshape((125,32))(x2)
x2 = mergeLayer(100)(x2)

#x = L.Multiply(x, x2)(x)
x = L.Multiply()([x,x2])

x = L.Flatten()(x)
x = L.Dense(400, activation='relu', name='dense1')(x) # Is relu used here?
x = L.Dropout(0.5)(x)
classify = L.Dense(numClasses, activation='softmax', name='dense2')(x)

model = M.Model(inputs=[source,source2],outputs=classify)
optimizer= O.SGD(momentum=0.02)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc'])

return model

Tags: nameselfinputoutputdefactivationkerneldropout
1条回答
网友
1楼 · 发布于 2024-09-30 16:23:28

以下是代码中的一些更正:

  • 您不需要output_dim**kwargs参数
  • 我已经用额外的维度定义了它,而不是在内核上使用expand_dims(但是您的keras版本的行为似乎与我的不同,所以使用#alternative行代码)。在
  • 主要问题:该batch_dot需要两个具有相同批量大小的张量(这意味着:第一个维度必须相同)
    • 通过重复内核以适应x的批大小来解决这个问题
  • 将所有tf函数与keras后端函数(import keras.backend as K)交换-这不是问题,但是您可以使用此方法将此解决方案移植到其他受支持的后端。在

一。在

class mergeLayer(Layer):

    #your init doesn't need output_dim and **kwargs
    def __init__(self):
        super(mergeLayer,self).__init__()
        self.kernel_initializer = INIT.get('uniform')

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(
                          name='kernel',

                          #corrected shape to avoid expand_dims
                          shape=(1,)+input_shape[1:],
                              #alternative:
                              #shape = input_shape[1:],
                          initializer=self.kernel_initializer,trainable=True)

        super(mergeLayer,self).build(input_shape) # Be sure to call this somewhere!

    def call(self, x):
        #take a tensor of ones with the same shape as x
        form = K.ones_like(x)

        #multiplies the kernel to match the batch size of x
        kernel = form * self.kernel
            #alternative:
            #kernel = form * K.expand_dims(self.kernel,0)

        #used K.permute_dimensions instead of tf
        temp = K.batch_dot(kernel,K.permute_dimensions(x,(0,2,1)))+1
        return temp

    def compute_output_shape(self, input_shape):
        return input_shape

相关问题 更多 >