PyTorch append tensor to a file iteratively

Viewed 392

I need to store intermediate results from a particular layer in my CNN encoder model for a different task. However, if I wait for the entirety of the model to finish running, GPU memory overflows. All the values to write are torch tensors. Currently, I focus on writing it into a text file as follows:

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")

How can I write the values after each loop, but require to append the values to the existing keys of the txt file.

0 Answers
Related