在隐藏的图层keras之后添加两层

时间:2019-10-04 08:56:38

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

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

     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'))

如何获得期望的模型?

对不起,如果我问一个愚蠢的问题,我是人工智能的初学者。

1 个答案:

答案 0 :(得分: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

希望这会有所帮助!