简体   繁体   English

如何在 tensorflow 的回调中访问训练和测试数据?

[英]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.

相关问题 批量培训但是在Tensorflow中测试单个数据项? - Training in batches but testing individual data item in Tensorflow? TensorFlow:如何创建训练和测试图像数据集 - TensorFlow: how to create training and testing image datasets 在Tensorflow中同时进行培训和测试 - Simultaneous training and testing in Tensorflow 如何将数据拆分为训练和测试 - how to split data into training and testing 如何从张量流上的MNIST测试中保存训练数据的权重以备将来使用? - How to save weights of training data from MNIST testing on tensorflow for future use? 如何在 keras 自定义回调中访问 tf.data.Dataset? - how to access tf.data.Dataset within a keras custom callback? 如何将数据拆分为训练和测试数据 - How to split the data into training and testing data 如何将我的训练数据上传到谷歌以进行 Tensorflow 云训练 - How to upload my training data into google for Tensorflow cloud training 如何将 TensorFlow 训练数据导出到 CSV - How to export TensorFlow training data to CSV 如何选择特定的数据点进行测试和训练? - How to select SPECIFIC data points for testing and training?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM