带StellarGraph的Watchyourstep模型在GPU上不工作

2024-05-16 19:44:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用WatchYourStep算法和StellarGraph来训练一个大型图嵌入

出于某种原因,该模型仅在CPU and上进行训练,而不使用GPU
使用:

  • TensorFlow gpu 2.3.1
  • 有2个GPU,cuda 10.1
  • 在nvidia docker容器中运行
  • 我知道tesnorflow确实找到了GPU。(tf.debugging.set_log_device_placement(True)
  • 我试图在with tf.device('/GPU:0'):下运行
  • 我试着用tf.distribute.MirroredStrategy()运行它
  • 尝试卸载tensorflow并重新安装tensorflow gpu

不过,在运行nvidia smi时,我在GPU上看不到任何活动,而且训练非常缓慢。
如何调试这个

def watch_your_step_model():
    '''use the config to geenrate the WatchYourStep model'''
    cfg = load_config()
    generator           = generator_for_watch_your_step()
    num_walks           = cfg['num_walks']
    embedding_dimension = cfg['embedding_dimension']
    learning_rate       = cfg['learning_rate']
    
    wys = WatchYourStep(
        generator,
        num_walks=num_walks,
        embedding_dimension=embedding_dimension,
        attention_regularizer=regularizers.l2(0.5),
    )
    
    x_in, x_out = wys.in_out_tensors()
    model = Model(inputs=x_in, outputs=x_out)
    model.compile(loss=graph_log_likelihood, optimizer=optimizers.Adam(learning_rate))
    return model, generator, wys

def train_watch_your_step_model(epochs = 3000):
    cfg = load_config()
    batch_size      = cfg['batch_size']
    steps_per_epoch = cfg['steps_per_epoch']
    callbacks, checkpoint_file = watch_your_step_callbacks(cfg)
    
    # strategy = tf.distribute.MirroredStrategy()
    # print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
    # with strategy.scope():
    
    model, generator, wys = watch_your_step_model()

    train_gen = generator.flow(batch_size=batch_size, num_parallel_calls=8)
    train_gen.prefetch(20480000)

    history = model.fit(
        train_gen, 
        epochs=epochs, 
        verbose=1, 
        steps_per_epoch=steps_per_epoch,
        callbacks = callbacks
    )
     
    copy_last_trained_wys_weights_to_data()
    
    return history, checkpoint_file

with tf.device('/GPU:0'):
    train_watch_your_step_model()

Tags: inyourmodelgputfsteptrainembedding