【问题标题】:Add two layers after a hidden layer keras在隐藏层keras之后添加两层
【发布时间】:2020-02-02 14:17:04
【问题描述】:

我正在尝试获得这样的神经网络模型:

     input
       |
     hidden
   /       \
hidden   output2
   |
output1

下面是一个简单的代码示例:

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # from here I would like to add a new neural network
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

如何得到预期的模型?

如果我问了一个愚蠢的问题,请见谅,我是人工智能的初学者。

【问题讨论】:

  • 我认为您必须从顺序模型转移到执行此操作。您的代码很可能类似于:x = Layer1(input); x = Layer2(x)...

标签: machine-learning keras neural-network artificial-intelligence conv-neural-network


【解决方案1】:

您可以使用 keras 函数式 API 而不是顺序 API 来完成它,如下所示:

from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D

num_classes = 10
inp= Input(shape=input_shape)
conv1 = Conv2D(32, kernel_size=(3,3), activation='relu')(inp)
conv2 = Conv2D(64, (3, 3), activation='relu')(conv1)
max_pool = MaxPooling2D(pool_size=(2, 2))(conv2)
flat = Flatten()(max_pool)

hidden1 = Dense(128, activation='relu')(flat)
output1 = Dense(num_classes, activation='softmax')(hidden1)

hidden2 = Dense(10, activation='relu')(flat)  #specify the number of hidden units
output2 = Dense(3, activation='softmax')(hidden2) #specify the number of classes

model = Model(inputs=inp, outputs=[output1 ,output2])

您的网络如下所示:

Model: "model_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_7 (InputLayer)            (None, 64, 256, 256) 0                                            
__________________________________________________________________________________________________
conv2d_10 (Conv2D)              (None, 62, 254, 32)  73760       input_7[0][0]                    
__________________________________________________________________________________________________
conv2d_11 (Conv2D)              (None, 60, 252, 64)  18496       conv2d_10[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_4 (MaxPooling2D)  (None, 30, 126, 64)  0           conv2d_11[0][0]                  
__________________________________________________________________________________________________
flatten_4 (Flatten)             (None, 241920)       0           max_pooling2d_4[0][0]            
__________________________________________________________________________________________________
dense_6 (Dense)                 (None, 128)          30965888    flatten_4[0][0]                  
__________________________________________________________________________________________________
dense_8 (Dense)                 (None, 10)           2419210     flatten_4[0][0]                  
__________________________________________________________________________________________________
dense_7 (Dense)                 (None, 10)           1290        dense_6[0][0]                    
__________________________________________________________________________________________________
dense_9 (Dense)                 (None, 3)            33          dense_8[0][0]                    
==================================================================================================
Total params: 33,478,677
Trainable params: 33,478,677
Non-trainable params: 0

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2019-01-27
    • 2019-05-19
    • 2019-08-21
    • 2018-01-25
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2017-07-31
    相关资源
    最近更新 更多