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.
Advertisement
Answer
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