使用Keras

时间:2018-04-04 13:07:26

标签: r tensorflow keras

当我创建模型时,我会这样做:

model <- keras_model_sequential()
model %>% 
  layer_1(...)
  layer_2(...)
  layer_3(...)

当我使用摘要时,summary(model)

Layer  -- Connected to
layer_1 --   
layer_2 -- layer_1
layer_3 -- layer_2

图层是一个接一个地添加的,但我该怎么做:

Layer  -- Connected to
layer_1 --   
layer_2 -- layer_1
layer_3 -- layer_1

我希望第2层和第3层连接到第1层。

2 个答案:

答案 0 :(得分:2)

您可以使用keras_model

执行此操作
library(keras)

layer_1 <- layer_input(1)

layer_2 <- layer_1 %>% layer_dropout(0.4)
layer_3 <- layer_1 %>% layer_dropout(0.6)

model <- keras_model(
  inputs = layer_1, 
  outputs = c(layer_2, layer_3)
)

summary(model)

输出:

___________________________________________________________________
Layer (type)          Output Shape  Param # Connected to           
===================================================================
input_1 (InputLayer)  (None, 1)     0                              
___________________________________________________________________
dropout_1 (Dropout)   (None, 1)     0       input_1[0][0]          
___________________________________________________________________
dropout_2 (Dropout)   (None, 1)     0       input_1[0][0]          
===================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
___________________________________________________________________

这个例子非常虚拟,只是为了说明这一点。

答案 1 :(得分:0)

代码片段的另一个例子:

input1 = keras.layers.Input(shape=(16,))
x1 = keras.layers.Dense(8, activation='relu')(input1)
x2 = keras.layers.Dense(8, activation='relu')(input1)
added = keras.layers.add([x1, x2])

model = keras.models.Model(inputs=[input1], outputs=added)

model.summary()

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_44 (InputLayer)           (None, 16)           0                                            
__________________________________________________________________________________________________
dense_65 (Dense)                (None, 8)            136         input_44[0][0]                   
__________________________________________________________________________________________________
dense_66 (Dense)                (None, 8)            136         input_44[0][0]                   
__________________________________________________________________________________________________
add_92 (Add)                    (None, 8)            0           dense_65[0][0]                   
                                                                 dense_66[0][0]                   
==================================================================================================
Total params: 272
Trainable params: 272
Non-trainable params: 0
__________________________________________________________________________________________________