【问题标题】:How can I change dataset at every epoch in keras?如何在 keras 的每个时期更改数据集?
【发布时间】:2019-12-02 11:59:09
【问题描述】:

假设我有noisy_features 和clean_features。顾名思义,noisy_features 是从嘈杂的输入样本中提取的,而 clean_features 是从干净的输入样本中提取的。 mix_rate 是任意数字,定义将使用多少嘈杂和干净的特征。我想用以下伪代码更改我用 keras 编写的网络:

train_network():
    clean_features = get_features(clean_inputs)
    noisy_features = get_features(noisy_inputs)
    number_of_mix_features = mixture_rate*number_of_features

    for epoch in number_of_epochs:
       random_indices = random(number_of_mix_features)
       input_features = clean_features
       input_features[random_indices]=noisy_features[random_indices]
       train_network()

函数train_network() 对应于keras 中的model.fit(),但不适用于此上下文,因为model.fit() 内部已经存在epoch 循环。

所以,问题是我无法在 keras 中的每个时期都更改输入特征。如果我在使用之前更改 input_featuresmodel.fit() random_indices 在整个训练期间将保持不变。

我尝试使用回调:

class Noisify(Callback):
    def __init__(self,mixture_rate = 0.2):
         self.mixture_rate = mixture_rate

    def on_epoch_begin(self,epoch,logs={}):
         # get input_tensor

         '''
         mix randomly noisy and clean features here

         '''

         # set input_tensor

但在这里我不知道获取和设置输入张量的方法,例如 model.get_layerget_weights

有什么建议吗?

【问题讨论】:

    标签: python tensorflow keras deep-learning


    【解决方案1】:

    我能想到的直接方法是,使用 pandas 函数 .sample() 对数据进行采样以构建混合数据集。有一个 for 循环来调用模型的时期数。

    所以你会有类似的东西:

    def data_resampler(): 
        # do data sampling using pandas sample function 
    
    for i in range(<number of epochs>): 
        mixed_data = data_resampler() 
        model(mixed_data) 
    

    如果您需要代码方面的帮助,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      相关资源
      最近更新 更多