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

时间:2018-03-16 14:01:36

标签: python regex

我的.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

有没有人可以解决这个问题?

1 个答案:

答案 0 :(得分:1)

代码的最后一部分应该是这样的:

epoch = re.findall(r"Epoch\s*\[(\d+)\/\d+\]", file)
epoch = np.array(epoch).astype(np.float32)
print(epoch)