首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在神经元子集上应用softmax

在神经元子集上应用softmax
EN

Stack Overflow用户
提问于 2017-12-05 04:04:57
回答 1查看 443关注 0票数 0

我在Keras中构建一个卷积网络,它为图像分配多个类。考虑到图像具有9兴趣点,可以通过以下三种方式之一来分类,我想添加27输出神经元,使其具有softmax激活,从而计算每一个连续的三重神经元的概率。

有可能这样做吗?我知道我可以简单地添加一个大的softmax层,但是这会导致在所有输出神经元上的概率分布,这对我的应用来说太宽了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-05 11:42:34

在最简单的实现中,您可以重塑数据,并得到您所描述的“每个连续三胞胎的概率”。

您可以使用27个类(形状类似于(batch_size,27) )来获取输出,并对其进行整形:

代码语言:javascript
复制
model.add(Reshape((9,3)))
model.add(Activation('softmax'))

还要注意重塑您的y_true数据。或者在模型中添加另一个重塑以恢复原始表单:

代码语言:javascript
复制
model.add(Reshape((27,))

在更详细的解决方案中,您可能会根据9个实例点的位置(如果它们具有大致静态的位置)并创建并行路径。例如,假设您的9个位置是间隔均匀的矩形,并且您希望对这些段使用相同的网络和类:

代码语言:javascript
复制
inputImage = Input((height,width,channels))

#supposing the width and height are multiples of 3, for easiness in this example
recHeight = height//3
recWidth = width//3

#create layers here without calling them
someConv1 = Conv2D(...)
someConv2 = Conv2D(...)
flatten = Flatten()
classificator = Dense(..., activation='softmax')

outputs = []
for i in range(3):
    for j in range(3):
        fromH = i*recHeight
        toH = fromH + recHeight
        fromW = j*recWidth
        toW = fromW + recWidth
        imagePart = Lambda(
                           lambda x: x[:,fromH:toH, fromW:toW,:], 
                           output_shape=(recHeight,recWidth,channels)
                          )(inputImage)

        #using the same net and classes for all segments
        #if this is not true, create new layers here instead of using the same
        output = someConv1(imagePart)
        output = someConv2(output)
        output = flatten(output)
        output = classificator(output)
        outputs.append(output)

outputs = Concatenate()(outputs)

model = Model(inputImage,outputs)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47645910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档