首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Keras Functional微调模型

使用Keras Functional微调模型
EN

Stack Overflow用户
提问于 2019-02-07 17:33:05
回答 1查看 535关注 0票数 1

我正在使用VGG16在我的数据集中完成它。

下面是模型:

代码语言:javascript
复制
def finetune(self, aux_input):
        model = applications.VGG16(weights='imagenet', include_top=False)
        # return model

        drop_5 = Input(shape=(7, 7, 512))
        flatten = Flatten()(drop_5)
        # aux_input = Input(shape=(1,))
        concat = Concatenate(axis=1)([flatten, aux_input])

        fc1 = Dense(512, kernel_regularizer=regularizers.l2(self.weight_decay))(concat)
        fc1 = Activation('relu')(fc1)
        fc1 = BatchNormalization()(fc1)

        fc1_drop = Dropout(0.5)(fc1)
        fc2 = Dense(self.num_classes)(fc1_drop)
        top_model_out = Activation('softmax')(fc2)

        top_model = Model(inputs=drop_5, outputs=top_model_out)

        output = top_model(model.output)

        complete_model = Model(inputs=[model.input, aux_input], outputs=output)

        return complete_model

我对模型有两个输入。在上面的函数中,我对扁平数组和我的aux_input使用串联。我不确定这是否适用于imagenet权重。

当我运行这个程序时,我会得到一个错误:

ValueError:图断开:不能获得张量张量(“aux_input:0”,shape=(?,1),dtype=float32)在"aux_input“层的值。访问前几个层时没有出现任何问题:“input_2”、“flatten_1”

不知道我哪里出了问题。

如果有关系,这就是适合的功能:

代码语言:javascript
复制
model.fit(x={'input_1': x_train, 'aux_input': y_aux_train}, y=y_train, batch_size=batch_size,
                    epochs=maxepoches, validation_data=([x_test, y_aux_test], y_test),
                    callbacks=[reduce_lr, tensorboard], verbose=2)

但是,当我调用fit函数时,会在这个model.summary()函数之前得到一个错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-07 20:08:40

问题是,您在aux_input中使用的是top_model,但没有将它指定为top_model定义中的输入。尝试用以下方法替换top_modeloutput的定义:

代码语言:javascript
复制
top_model = Model(inputs=[drop_5, aux_input], outputs=top_model_out)
output = top_model([model.output, aux_input])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54579078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档