borchero / pycave

Traditional Machine Learning Models for Large-Scale Datasets in PyTorch.
https://pycave.borchero.com
MIT License
126 stars 13 forks source link

How can i disable model output prints during fit_preditct? #13

Closed mayurgd closed 2 years ago

mayurgd commented 2 years ago

I have disabled progress bar using enable_progress_bar=0, but not able to find the correct parameter that will disable the internal model output printing. Just want to disable those coz I will be running it a lot of times, and don't want the output console to be printed with all the models internal outputs

import torch
import pandas as pd
from pycave.clustering import KMeans
from pycave.bayes import GaussianMixture

X = torch.cat([
    torch.randn(1000, 6) - 5,
    torch.randn(1000, 6),
    torch.randn(1000, 6) + 5,
])

estimator = KMeans(num_clusters = 4, trainer_params=dict(gpus=1, 
                                                         enable_progress_bar=0, 
                                                         max_epochs=100,
                                                         enable_model_summary=False))
labels = estimator.fit_predict(X).numpy()

print(pd.value_counts(labels))

Current Output:

Running initialization...
Fitting K-Means...
{'batch_size': 3000, 'collate_fn': <function collate_tensor at 0x0000015AC3F37C10>}
{'batch_size': 3000, 'collate_fn': <function collate_tensor at 0x0000015AC3F37C10>}
{'batch_size': 1, 'sampler': None, 'batch_sampler': <pytorch_lightning.overrides.distributed.IndexBatchSamplerWrapper object at 0x0000015A92D84040>, 'collate_fn': <function collate_tensor at 0x0000015AC3F37C10>, 'shuffle': False, 'drop_last': False}
1    1000
0    1000
3     509
2     491
dtype: int64

Expected Output:

1    1000
0    1000
3     509
2     491
dtype: int64
borchero commented 2 years ago

Hey @mayura64! The dictionaries that are printed should never be there, that's a bug in a dependency. Thanks for finding that, it's resolved now :)

As far as the PyCave logs are concerned (Running initialization... and Fitting K-Means...), you can turn them off easily by adding the following:

import logging
from pycave import set_logging_level

set_logging_level(logging.WARNING)

The logs that are printed have level logging.INFO. I'll close this as soon as I published a new PyCave version which fixes the dependency.

borchero commented 2 years ago

Note that set_logging_level(logging.WARNING) also turns off the progress bar and the model summary automatically so you don't have to set these flags explicitly.

AndreasGerken commented 7 months ago

Is this part somewhere in the documentation? Didnt find it and used up too much time to find out how to suppress the outputs. The solution also does not suppress the "PossibleUserWarning" if you dont want to use the CPU.

This is my solution now:

import pycave
from pycave import set_logging_level
import torch

pycave.set_logging_level(logging.WARNING)
gmm = pycave.bayes.GaussianMixture(num_components=2)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    gmm.fit(torch.randn(100, 2).float())