PyTorch将张量迭代地附加到文件中

2024-09-27 02:17:43 发布

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

我需要在CNN编码器模型中为不同的任务存储来自特定层的中间结果。但是,如果我等待整个模型完成运行,GPU内存就会溢出。所有要写入的值都是火炬张量。 目前,我的重点是将其写入文本文件,如下所示:

def train(loop):
  for i, batch in enumerate(train_loader, 0):
    store_dictionary['images'] = images
    store_dictionary['recon'] = recon

  #after the train loop is complete
  try:
    output_file = open('stored_dictionary_values.txt', 'wt')
    output_file.write(str(store_dict))
    output_file.close()
  except:
    print("Unable to write to file")

如何在每次循环后写入值,但需要将值附加到txt文件的现有键中


Tags: tostore模型txtloopoutputdictionarygpu

热门问题