import os
import torch
import numpy as np
import json
# 初始化数据集
dataset = ""
# 遍历文件夹下的所有文件
folder_path = 'H:/学习资料/代码/python/jupyterlab_project/pytorch/log/'
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
# 如果文件是 txt 格式,则读取其内容并将其添加到数据集中
if file_name.endswith('.txt'):
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
dataset += text
print(dataset[0:1500])
tensor_list = dataset.split('\n')
print(tensor_list[0:20])
t_len = len(tensor_list)
for i in range(t_len):
if 0 == i:
tensor_set = torch.tensor([float(x) for x in tensor_list[i].strip('[]').split(',')]).unsqueeze(0)
try:
tensor_i = torch.tensor([float(x) for x in tensor_list[i].strip('[]').split(',')]).unsqueeze(0)
tensor_set = torch.cat((tensor_set,tensor_i),0)
except:
print(i)
print('异常中断')
# print(tensor_list[i])
print(tensor_set[0:5])
输出结果如下图: