我如何解释keras中合并模型的模型摘要?

2022-12-05 02:58:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想建立一个模型,将许多较小模型的输出合并为一个。我想要146个网络,每个网络有17个输入,并给出一个概率作为输出。所有这些网络的输出需要合并并作为单个单元使用。为此,我做了如下操作:

def build(layer_str,actv):
        #take the input layer structure and convert it into a list
        layers=layer_str.split("-")
        #print(layers)
        #convert the strings in the list to integer
        layers=list(map(int,layers))
              
        #let's build our model
        model= tf.keras.Sequential()
        
        #we add the first layer and the input layer to our network
        model.add(Dense(layers[1],input_shape=(layers[0],),activation=actv[0]))
        
        #we add the hidden layers 
        for (x,i) in enumerate(layers):
            if(x>1 and x!=(len(layers)-1)):
                model.add(Dense(i,activation=actv[x]))
                
        
        #then add the final layer        
        model.add(Dense(layers[-1],activation=actv[-1]))        
        
        #return the construtcted model
        return model 

然后,合并模型如下:

def Merge_model(layer,act,data,label,lr,epochs,batch_size):
    model_list=[]
    for i in range(146):
        model=nn.build(layer,act)
        model_list.append(model)
    merged_layers = concatenate([model_list[i].output for i in range(146)])
    x = merged_layers
    out = Activation('sigmoid')(x)
    merged_model = Model([model_list[i].input for i in range(146)], [out])
    print(merged_model.summary())
    merged_model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    result,predictions=nn.train_eval(data,label,merged_model,lr,epochs,batch_size)
data=np.random.rand(10,146,17)
data=[d for d in data]
label=np.random.randint(0,1,(10,146,1))
label=[lb for lb in label]
print(len(label[0]))
lr=0.01
epochs=100
batch_size=16
Merge_model("17-7-1",["relu","sigmoid"],data,label,lr,epochs,batch_size)

我得到了这样的模型摘要,但不明白如何理解它。我的训练数据和图层的形状应该是什么? https://drive.google.com/file/d/1juffdLY0i9f9rgldKfHG_MYXCK8wBV09/view?usp=sharing


Tags: thein模型addlayerforinputdatamodellayersmergedlabellistlractv