简体   繁体   English

访问损耗指标来自 keras model

[英]Access loss metric from keras model

I am trying to access the training loss of my model which I have built, as I call a training loop on it.我正在尝试访问我构建的 model 的训练损失,因为我称之为训练循环。 I have built a model as follows:我已经构建了一个 model 如下:

    def create_model(self):
        
        model = Sequential()
        model.add(Dense(172, input_shape=(1, 172), activation = 'relu'))
        model.add(Dense(172, activation = 'relu'))
        model.add(Dense(86, activation = 'relu'))
        model.add(Dense(43, activation = 'relu'))
        model.add(Dense(21, activation = "relu"))
        model.add(Flatten())
        model.add(Dense(7, activation="linear"))
        model.compile(loss="mse", optimizer=Adam(lr=0.001), metrics=['accuracy'])
        return model

And my fit is as follows:我的适合如下:

self.model.fit(np.array(X), np.array(y), batch_size=MINIBATCH_SIZE, verbose=0, shuffle=False) 

The model is fit on a batch, and is part of a larger class (DQN Agent). model 适合批量生产,是更大的 class(DQN 代理)的一部分。

As I run a fit every timestep, and I have multiple timesteps per episode, how can I have a variable I can access in the training loop which gives me the loss of the network, so I can monitor it more closely?当我在每个时间步都运行一个合适的,并且我每集有多个时间步时,我怎样才能有一个我可以在训练循环中访问的变量,这让我失去了网络,所以我可以更密切地监控它? (with an end goal of adding it to tensorboard) (最终目标是将其添加到张量板)

I can try to either access this every step or every episode (300 steps), whatever works easier.我可以尝试访问每一步或每集(300 步),无论哪种更容易。

You can either change your verbose to 1 (shows continuously changing loss and progress within epoch) or change it to 2 (shows loss after each epoch).您可以将详细信息更改为 1(显示在 epoch 内不断变化的损失和进度)或将其更改为 2(在每个 epoch 后显示损失)。 You can also change your fit line to:您还可以将拟合线更改为:

history = self.model.fit(np.array(X), np.array(y), batch_size=MINIBATCH_SIZE,verbose=0, shuffle=False)

Then after n_epochs , you can extract the loss in an array with the following call:然后在n_epochs之后,您可以使用以下调用提取数组中的损失:

losses = history.history['loss']

You can iterate through training in a loop, to extract the losses however frequently you would like, and can either plot them, or save them to look at them separately.您可以在循环中迭代训练,以您希望的频率提取损失,并且可以 plot 或保存它们以单独查看它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM