【问题标题】:How can I extract Flatten Layer Output for each epoch?如何为每个时期提取展平层输出?
【发布时间】:2020-07-15 13:06:26
【问题描述】:
model = Sequential()
model.add(Conv2D(50, (5,5), activation='relu', input_shape =(5,5,1), kernel_initializer='he_normal'))

model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.summary()

# compile the model
model.compile(loss='binary_crossentropy', optimizer= 'adam', metrics=['accuracy'])
model_checkpoint=ModelCheckpoint(r'C:\Users\globo\Desktop\Test_CNN\Results\Kernel5x5\Weights'+'\\'+test+'\model_test{epoch:02d}.h5',save_freq=1,save_weights_only=True)

# fit the model
history = model.fit(X_train, Y_train, epochs=10, batch_size=32, verbose=1, callbacks=[model_checkpoint], shuffle=True, validation_split=0.5)

我已经使用“ModelCheckpoint”为每个 epoch 提取权重,但是如何为每个 epoch 提取 flatten 层输出并保存它们?

【问题讨论】:

    标签: python tensorflow keras flatten conv-neural-network


    【解决方案1】:

    用顺序模型这样做根本不可行。 你应该使用函数式 API

    inp = Input((5,5,1))
    x = Conv2D(50, (5,5), activation='relu', kernel_initializer='he_normal')(inp)
    xflatten = Flatten()(x)
    out = Dense(1, activation='sigmoid')(xflatten)
    
    main_model = Model(inp, out) # this works same as your model
    flatten_model = Model(inp, xflatten) # and this only outputs the flatten layer and is not necessary to compile it because we won't train it, it just shows the output of a layer
    
    main_model.compile(loss='binary_crossentropy', optimizer= 'adam', metrics=['accuracy'])
    history = main_model.fit(X_train, Y_train, epochs=10, batch_size=32, verbose=1, callbacks=[model_checkpoint], shuffle=True, validation_split=0.5)
    

    查看展平层的输出:

    flatten_model.predict(X)
    

    【讨论】:

    • 首先,感谢您的回答。然后,我真的很喜欢你的解决方案,但我怎样才能获得每个时代的输出时代? (所以在训练期间)
    • 谢谢,只需删除 fit 函数的 'epoch' 属性并将这一行:“history = ...”放入 for 循环并打印 model.predict 输出(用于获取层输出)或 model.evaluate(用于获取 loss 和 acc)每次迭代的 flatten 模型。就像 Keras 的时代一样,不会伤害任何东西
    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 2017-04-24
    • 2019-03-18
    • 2019-09-11
    • 2019-02-04
    • 2023-03-12
    • 2020-06-22
    • 1970-01-01
    相关资源
    最近更新 更多