keras CNN中2层之间的元素明智加权平均误差 [英] Error in element wise weighted averaging between 2 layers in keras cnn

查看:190
本文介绍了keras CNN中2层之间的元素明智加权平均误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cnn
中2层之间的元素明智加权平均中遇到错误我的基本模型是

I am getting error in element wise weighted averaging between 2 layers in cnn My base model is

model_base = Sequential()
# Conv Layer 1
model_base.add(layers.SeparableConv2D(32, (9, 9), activation='relu', input_shape=input_shape))
model_base.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

# Conv Layer 2
model_base.add(layers.SeparableConv2D(64, (9, 9), activation='relu'))
model_base.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

# Conv Layer 3
model_base.add(layers.SeparableConv2D(128, (9, 9), activation='relu'))
model_base.add(layers.MaxPooling2D(2, 2))
# model.add(layers.Dropout(0.25))

model_base.add(layers.Conv2D(256, (9, 9), activation='relu'))
# model.add(layers.MaxPooling2D(2, 2))
# Flatten the data for upcoming dense layer
#model_base.add(layers.Flatten())
#model_base.add(layers.Dropout(0.5))
#model_base.add(layers.Dense(512, activation='relu'))

print(model_base.summary())

我要取出第2、4和6层,做一个点积,然后激活并调整大小。现在,我想获取a1和l1的元素明智加权平均值。但是没有得到,因为批次的尺寸不匹配。有人可以帮忙吗?

I am taking out layer 2,4 and 6, doing a dot product, followed by activation and resizing. Now I would like to get element wise weighted average of a1 and l1. But not getting since the dimensions of the batches does not match. Can anyone help?



l1 = model_base.layers[2].output 
l1 = GlobalAveragePooling2D()(l1) 
c2 = model_base.layers[4].output
c2 = GlobalAveragePooling2D()(c2) 
c3 = model_base.layers[6].output
#c3 = GlobalAveragePooling2D()(c3) 
#c=c3.shape[-1]

l1 = Dense(512)(l1)
c2 = Dense(512)(c2) 

c13 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp1')([l1, c3])  # batch*x*y
c23 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp1')([c2, c3])  # batch*x*y

flatc13 = Flatten(name='flatc1')(c13)  # batch*xy
flatc23 = Flatten(name='flatc1')(c23)  # batch*xy

a1 = Activation('softmax', name='softmax1')(flatc13)
a2 = Activation('softmax', name='softmax1')(flatc23)



#a1 = Activation('softmax', name='softmax1')(c13)
#a2 = Activation('softmax', name='softmax1')(c23)

from keras.layers.core import Reshape


reshaped1 = Reshape((-1,512), name='reshape1')(l1)  # batch*xy*512
reshaped2 = Reshape((-1,512), name='reshape2')(c2)  # batch*xy*512
                                                                          
g1 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), name='g1')([reshaped1,a1])  # batch*512.```


推荐答案

为您的 base_model 赋予了构建下面代码块的正确方法...

given your base_model this the correct way to build the code block below...

l1 = model_base.layers[2].output
l1 = GlobalAveragePooling2D()(l1) 
c2 = model_base.layers[4].output
c2 = GlobalAveragePooling2D()(c2) 
c3 = model_base.layers[6].output

c = c3.shape[-1] ### this is important for the dimesionality
l1 = Dense(c)(l1)
c2 = Dense(c)(c2) 

c13 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), 
                                            elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp1')([c3, l1])  # batch*x*y

c23 = Lambda(lambda lam: K.squeeze(K.map_fn(lambda xy: K.dot(xy[0], xy[1]), 
                                            elems=(lam[0], K.expand_dims(lam[1], -1)), dtype='float32'), 3), name='cdp2')([c3, c2])  # batch*x*y

flatc13 = Flatten(name='flatc1')(c13)  # batch*xy
flatc23 = Flatten(name='flatc2')(c23)  # batch*xy

a1 = Activation('softmax', name='softmax1')(flatc13) # batch*xy
a2 = Activation('softmax', name='softmax2')(flatc23) # batch*xy

reshaped = Reshape((-1,c), name='reshape1')(c3)  # batch*xy*c

g1 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), 
            name='g1')([a1,reshaped])  # batch*c
g2 = Lambda(lambda lam: K.squeeze(K.batch_dot(K.expand_dims(lam[0], 1), lam[1]), 1), 
            name='g2')([a2,reshaped])  # batch*c

注意维数(在您的情况下,您不能使用512而是使用256进行操作,这由 c 变量)。还应注意Lambda操作中使用的层的顺序(例如,在c13中为([[c3,l1])而不是([ l1,c3])

pay attention to the dimensionality (in your case you can't operate with 512 but with 256, this is handled automatically by the c variable). pay attention also to the order of the layer used in the Lambda operations (for example in c13 it's ([c3, l1]) and not ([l1, c3]))

正在运行的笔记本在这里: https://colab.research.google.com/drive/1m0pB5GlYRtIsOnHUTz6LxRQblcvtVU3Y?usp=sharing

here the running notebook: https://colab.research.google.com/drive/1m0pB5GlYRtIsOnHUTz6LxRQblcvtVU3Y?usp=sharing

这篇关于keras CNN中2层之间的元素明智加权平均误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆