I want to be able to save GridSearchCV output to file while running.
GridSearchCV(XGBClassifier(), tuned_parameters, cv=cv, n_jobs=-1, verbose=10)
This is an example for an output:
Fitting 1 folds for each of 200 candidates, totalling 200 fits
[Parallel(n_jobs=-1)]: Using backend with 4 concurrent workers.
[CV] colsample_bytree=0.7, learning_rate=0.05, max_depth=4, n_estimators=300, subsample=0.7
[CV] colsample_bytree=0.7, learning_rate=0.05, max_depth=4, n_estimators=300, subsample=0.7
score=0.645, total= 6.3min
[Parallel(n_jobs=-1)]: Done 1 tasks | elapsed: 6.3min
I managed to save the first line and the Parallel lines, but no matter what I tried, I couldn't save the lines that start with [CV]. I want to save those lines so if the program will fail, I could at least see part of the results.
I tried the solutions from here
sys.stdout = open('file', 'w')
and:
with open('help.txt', 'w') as f:
with redirect_stdout(f):
print('it now prints to `help.text`')
This solution (that is also referring to this solution) also didn't work:
class Tee(object):
def __init__(self, *files):
self.files = files
def write(self, obj):
for f in self.files:
f.write(obj)
f.flush() # If you want the output to be visible immediately
def flush(self) :
for f in self.files:
f.flush()
And tried this monkey-patch as the author called it, but is also just saved the "Parallel" lines.
(Just to emphasize, the codes above are just a glimpse of the proposed solutions, when I tried them, I took all relevant code).
Is there a way to save ALL output?
I don't know if you can do this using
sys
library or others. Instead, I suggest the following approach where we redirect stdout and stderr properly.Suppose you have a script like this:
test.py
Then run it with:
Then you will have both "Parallel" and "CV" lines in
logfile.txt
:Details
The "[CV]" lines are produced by
print
statement (Source). This is written to stdout.And "Parallel" lines are produced by loggers (Source). This is written to stderr.
> logfile.txt 2>&1
is a trick to redirect both stdout and stderr to a same file (Related question). As a result, both messages are written to a same file.