[英]How to access the training and testing data within a callback in tensorflow?
> import tensorflow as tf
>
> class MyMetric(tf.keras.callbacks.Callback):
> def on_epoch_end(self,epoch,logs={}):
> # how to access X_train and X_val here
>
> ...
> model.fit(X_train,y_train,batch_size=32,epochs=10,validation_data=(X_val,y_val),shuffle=True,callbacks=[MyMetric()]
I am trying to implement a custom metric in tensorflow 2.0 using a callback.我正在尝试使用回调在 tensorflow 2.0 中实现自定义指标。 Within the
on_epoch_end
method I need to access the training and validation data (the entire samples, not batches) as provided to the fit method.在
on_epoch_end
方法中,我需要访问提供给 fit 方法的训练和验证数据(整个样本,而不是批次)。 Is there any way to do this?有没有办法做到这一点? Thanks!
谢谢!
Accept training and test dataset as initialisation argument to your custom callback class and then use it in your on_epoch_end method.接受训练和测试数据集作为自定义回调类的初始化参数,然后在 on_epoch_end 方法中使用它。
Something like this像这样的东西
class MyMetric(keras.callbacks.Callback):
def __init__(self, X_test):
self.X_test = X_test
And while calling fit, pass test set as argument to your custom callback as below在调用 fit 时,将测试集作为参数传递给您的自定义回调,如下所示
model.fit(X_train,y_train,batch_size=32,epochs=10,validation_data=(X_val,y_val),shuffle=True,callbacks=[MyMetric(X_test)]
More details on https://keras.io/guides/writing_your_own_callbacks/有关https://keras.io/guides/writing_your_own_callbacks/ 的更多详细信息
You can edit the .fit function and pass in an extra list or queue, then pass the extra argument into the callback function... Probably a queue, then have another thread or function process the queue.您可以编辑 .fit 函数并传入一个额外的列表或队列,然后将额外的参数传递给回调函数......可能是一个队列,然后让另一个线程或函数处理该队列。
I did a similar modification to the Paramiko library and it worked well 😁我对 Paramiko 库做了类似的修改,效果很好😁
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.