我正在第二层中使用自定义连接的模型。以下代码有效,但我无法保存第二层。我尝试分别保存模型架构和权重,但是不起作用。创建自定义图层可能有助于保存带有权重的模型。我想为第二层创建一个自定义层,但我不确定如何使用keras自定义层来完成它。有人可以提供一些与保存模型或创建自定义图层有关的解决方案吗?
input_data = Input(shape=(28, 28,1))
first_layer = Conv2D(filters=16, kernel_size=(5,5), activation='relu', padding='same', input_shape = input_shape, use_bias = False)(input_data)
avg_1 = AveragePooling2D(pool_size=(2,2), data_format= 'channels_last')(first_layer)
add = []
random_matrix = np.random.randint(0,15,(4,32))
for i in range(32):
group = []
for j in range(4):
group_channel = Lambda(lambda x: x[:,:,:,random_matrix[j][i],np.newaxis])(avg_1)
conv_group = Conv2D(1, kernel_size=[5,5], strides=(stride,stride), activation = 'relu',padding='valid', data_format= 'channels_last',name = 'Conv_'+str(i*4+j)+'_Sparse', use_bias =False)(group_channel)
group.append(conv_group)
add_group = Add()(group)
add.append(add_group)
second_layer = Concatenate()(add)
avg_2 = AveragePooling2D(pool_size=(2,2), data_format= 'channels_last')(second_layer)
flatten = Flatten()(avg_2)
dense = Dense(num_classes, name = 'dense_3', use_bias = False)(flatten)
activation = Activation('softmax')(dense)
model = Model(input_data, activation)