Home > Enterprise >  How can we Read just float values from the lines of a file?
How can we Read just float values from the lines of a file?

Time:05-10

I want to read a file line by line and use some elements written in that file as the learning rate, epochs, and batch size in my neural network to configure it. My code is like this:

file_config = open("myfile", "r")
lines = myfile.readlines()
for line in lines: 
  print(line)

and the result is like this:

--learning_rate 0.1 --epochs 300 
--learning_rate 0.01 --epochs 300 
--learning_rate 0.1 --epochs 500 
--learning_rate 0.01 --epochs 500

Do you have any idea how I can assign the values written in each line to my learning rate and epochs of the model? In fact, how to retrieve the values from the lines of the file?

  • I tried data = line.split('--') but the result is not what I want.

CodePudding user response:

Maybe you can use like this:

import re
file_config = open("myfile", "r")
lines = myfile.readlines()
for line in lines:
    nums = re.findall(r'\d \.\d |\d ', line)
    print(f"learning_rate:{nums[0]} and  epochs:{nums[1]}")

This is the result :

learning_rate:0.1 and  epochs:300
learning_rate:0.01 and  epochs:300
learning_rate:0.1 and  epochs:500
learning_rate:0.01 and  epochs:500

CodePudding user response:

If the file format is fixed, you can get the values of the learning rates and epochs by index.

data.txt:

--learning_rate 0.1 --epochs 300
--learning_rate 0.01 --epochs 300
--learning_rate 0.1 --epochs 500
--learning_rate 0.01 --epochs 500

Code:

def read_lr_epochs(file_path):
    data = []
    with open(file_path) as data_file:
        lines = data_file.readlines()
        for line in lines:
            line = line.strip()
            if line:
                line = line.split(" ")
                data.append([float(line[1]), int(line[3])])
    return data


if __name__ == "__main__":
    print(read_lr_epochs("data.txt"))

Output:

[[0.1, 300], [0.01, 300], [0.1, 500], [0.01, 500]]

Then you can access the value of the list as required.

  • Related