TensorFlow:回调方法`ON_TRAIN_BATCH_END`比批量时间慢 [英] tensorflow:Callback method `on_train_batch_end` is slow compared to the batch time

查看:22
本文介绍了TensorFlow:回调方法`ON_TRAIN_BATCH_END`比批量时间慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用RandomSearch创建CNN模型,但速度非常慢,并弹出此错误tensorflow:Callback method on_train_batch_end is slow compared to the batch time 我在Google CoLab中运行我的代码,并在GPU上设置了硬件加速 这是我的代码

def model_builder(hp):
    model=Sequential([
        Conv2D(filters=hp.Int('conv_1_filter',min_value=32,max_value=128,step=32),
               kernel_size=hp.Int('conv_1_filter',min_value=2,max_value=3,step=1),
               activation='relu',
               padding='same',
               input_shape=(200,200,3)),
        MaxPooling2D(pool_size=(2,2),strides=(2,2)),
        
        Conv2D(filters=hp.Int('conv_2_filter',min_value=32,max_value=128,step=32),
               kernel_size=hp.Int('conv_2_filter',min_value=2,max_value=3,step=1),
               padding='same',
               activation='relu'),
        MaxPooling2D(pool_size=(2,2),strides=(2,2)),
        
        Flatten(),
        
        Dense(units=hp.Int('dense_1_units',min_value=32,max_value=512,step=128),
              activation='relu'),
        
        Dense(units=10,
              activation='softmax')
               
    ])
    
    model.compile(optimizer=Adam(hp.Choice('learning_rate',values=[1e-1,1e-3,3e-2])),
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    return model

然后随机搜索并调整

tuner=RandomSearch(model_builder,
                   objective='val_accuracy',
                   max_trials=2,
                   directory='projects',
                   project_name='Hypercars CNN'
                  )
tuner.search(X_train,Y_train,epochs=2,validation_split=0.2)

推荐答案

这是在每个批处理末尾运行的其他操作比批处理本身消耗更多时间时导致的。 可能是您的批次非常小,即任何与原始批次相比速度较慢的操作。

Increasing the batch size应解决此问题,或者您可以在model.fit()use_mutiprocessing = True并选择适当数量的员工以更高效地生成培训批次。

两个讨论此问题的帖子:

  1. Thread 1
  2. Thread 2

这篇关于TensorFlow:回调方法`ON_TRAIN_BATCH_END`比批量时间慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆