python正则表达式读取[1/300]

2021-09-16 23:07:45 发布

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

我有一个.txt文件

Epoch [0/150]   Iteration [0/313]   Training Loss 5.3858    Training Accuracy 0.781 Epoch [0/150]   Iteration [100/313] Training Loss 5.7784    Training Accuracy 1.713 Epoch [0/150]   Iteration [200/313] Training Loss 5.1698    Training Accuracy 3.055 Epoch [0/150]   Iteration [300/313] Training Loss 4.9572    Training Accuracy 4.421 Validation Accuracy 15.625  Epoch [1/150]   Iteration [0/313]   Training Loss 4.7359    Training Accuracy 13.672    Epoch [1/150]   Iteration [100/313] Training Loss 4.4300    Training Accuracy 10.694    Epoch [1/150]   Iteration [200/313] Training Loss 4.2356    Training Accuracy 11.682    Epoch [1/150]   Iteration [300/313] Training Loss 3.9475    Training Accuracy 12.782    Validation Accuracy 35.547  Epoch [2/150]   Iteration [0/313]   Training Loss 3.9055    Training Accuracy 17.578    Epoch [2/150]   Iteration [100/313] Training Loss 3.8225    Training Accuracy 17.791    Epoch [2/150]   Iteration [200/313] Training Loss 3.7882    Training Accuracy 18.039    Epoch [2/150]   Iteration [300/313] Training Loss 3.8982    Training Accuracy 18.542    Validation Accuracy 30.859  Epoch [3/150]   Iteration [0/313]   Training Loss 3.5016    Training Accuracy 22.656    Epoch [3/150]   Iteration [100/313] Training Loss 3.6835    Training Accuracy 22.095    Epoch [3/150]   Iteration [200/313] Training Loss 3.3630    Training Accuracy 22.225    Epoch [3/150]   Iteration [300/313] Training Loss 3.5110    Training Accuracy 22.366    Validation Accuracy 50.000  Epoch [4/150]   Iteration [0/313]   Training Loss 3.4000    Training Accuracy 26.562    Epoch [4/150]   Iteration [100/313] Training Loss 3.3732    Training Accuracy 25.259    

我想宣读所有的时代,并有损失,培训和验证精度相关的每个时代。你知道吗

下面返回一个精度列表,但不返回年代。你知道吗

train_accuracy = re.findall(r"Training Accuracy\s*(\d[.\d]*)", file)
train_accuracy = np.array(train_accuracy).astype(np.float32)
print(train_accuracy)

val_accuracy = re.findall(r"Validation Accuracy\s*(\d[.\d]*)", file)
val_accuracy = np.array(val_accuracy).astype(np.float32)
print(val_accuracy)

epoch = re.findall(r"Epoch\s*(\d+)", file)
epoch = np.array(epoch).astype(np.float32)
print(epoch)  #returns empty list

有人能解决这个问题吗?你知道吗