随机数列要求
- 长度 10
- 元素随机
- 每个数列中有两个相同的元素
- 生成到100个文本中
- 每个文本数列数量为 1000
代码
import torch
import pickle
import numpy as np
import os
import shutil
import logging
def creat_logger(log_path,logging_name,suf_name):
if not os.path.exists(log_path):
os.makedirs(log_path)
log_full_path = log_path + logging_name + suf_name
logger = logging.getLogger(logging_name)
logger.setLevel(level=logging.DEBUG)
handler = logging.FileHandler(log_full_path, encoding='UTF-8',mode = 'w')
handler.setLevel(logging.INFO)
logger.addHandler(handler)
return logger
def generate_tensor():
tensor = torch.zeros(11)
for i in range(10):
tensor[i] = torch.randn(1)
indices = torch.randperm(10)
indices = indices[:2].clone()
tensor[indices] = tensor[indices[0]].clone()
tensor[10] = tensor[indices[0]].clone()
return tensor
tensor = generate_tensor()
print(tensor)
num_tensors = 1000
path = './log1/'
if os.path.exists(path):
shutil.rmtree(path)
for j in range(100):
log_trace = creat_logger(path,'j_loop'+str(j),'.txt')
for i in range(num_tensors):
tensor = generate_tensor()
log_trace.info(tensor)
for handler in list(log_trace.handlers):
log_trace.removeHandler(handler)