# Example code for how to set up logging processes (cross validation folds) and grouping them?

**URL:** <https://community.wandb.ai/t/example-code-for-how-to-set-up-logging-processes-cross-validation-folds-and-grouping-them/1908>\
**Category:** W&B Help\
**Tags:** wandb\
**Created:** [February 12, 2022, 3:37pm UTC](https://community.wandb.ai/t/example-code-for-how-to-set-up-logging-processes-cross-validation-folds-and-grouping-them/1908 "2022-02-12T15:37:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![olto](https://avatars.discourse-cdn.com/v4/letter/o/2acd7d/32.png) [@olto](https://community.wandb.ai/u/olto)\
**Post date:** [February 12, 2022, 3:37pm UTC](https://community.wandb.ai/t/example-code-for-how-to-set-up-logging-processes-cross-validation-folds-and-grouping-them/1908/1 "2022-02-12T15:37:40Z")

</div>

Hi,

I am new to wandb and I am trying to figure out how to set up logging processes (based on cross validation folds) and to group them. What I would like to do is to plot/visualise performances for each fold in a cross validation scheme.

In this Colab example notebook

> **[Google Colaboratory](https://colab.research.google.com/github/wandb/examples/blob/master/colabs/scikit/Simple_Scikit_Integration.ipynb)**

at the very bottom the section “Basic Setup” says in point 2: "Groups: For multiple processes or cross validation folds, log each process as a runs and group them together. `wandb.init(group='experiment-1')`". I am not quite sure how to do this. I searched the documentation, but I was not successful. Can anyone point me to some example code how to do this?

Basically, what I am interested in is to visualise ROC, etc. for each fold and compare how much they differ.

Thanks in advance!  
Oliver

---

<div class="post-metadata">

**Author:** ![nathank](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nathank](https://community.wandb.ai/u/nathank)\
**Post date:** [February 15, 2022, 3:24pm UTC](https://community.wandb.ai/t/example-code-for-how-to-set-up-logging-processes-cross-validation-folds-and-grouping-them/1908/2 "2022-02-15T15:24:51Z")

</div>

Hi @olto,  
Thank you for trying out Weights and Biases! If you call

`run = wandb.init(project='my_project', group='my_group')`

from within a KFold `for` loop to start a new run for each fold and then

`wandb.log(﻿{﻿"roc" : wandb.plot.roc_curve(ground_truth, predictions)﻿}﻿) `

to log the ROC curves to that run

Here is what that may look like:

```auto
k_fold = KFold(n_splits=5)

for train_idx, test_idx in k_fold.split(X,y):
    X_train, X_test= X[train_idx], X[test_idx] 
    y_train, y_test= y[train_idx], y[test_idx]
    run = wandb.init(project='my_project', group='my_group')
    model = # build model
    model = model.fit(X_train,y_train)
    y_hat = model.predict_proba(X_test)

    wandb.log(﻿{﻿"roc" : wandb.plot.roc_curve(y, y_hat﻿)﻿}﻿)

    wandb.finish()

```

Also [here](https://github.com/wandb/examples/blob/master/examples/wandb-sweeps/sweeps-cross-validation/train-cross-validation.py) is a more advanced example using sweeps. Let me know if this helps to clarify or if you have any more questions about this.

Thank you,  
Nate

---

<div class="post-metadata">

**Author:** ![sydholl](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sydholl](https://community.wandb.ai/u/sydholl)\
**Post date:** [February 15, 2022, 3:25pm UTC](https://community.wandb.ai/t/example-code-for-how-to-set-up-logging-processes-cross-validation-folds-and-grouping-them/1908/3 "2022-02-15T15:25:10Z")

</div>

Hi @olto,  
Thank you for trying out Weights and Biases! If you call  
`run = wandb.init(project='my_project', group='my_group')`

from within a KFold `for` loop to start a new run for each fold and then

`wandb.log(﻿{﻿"roc" : wandb.plot.roc_curve( ground_truth, predictions, labels=﻿labels﻿)﻿}﻿) `

to log the ROC curves to that run

Here is what that may look like:

```
k_fold = KFold(n_splits=5)

for train_idx, test_idx in k_fold.split(X,y):
    X_train, X_test= X[train_idx], X[test_idx]
    y_train, y_test= y[train_idx], y[test_idx]
    run = wandb.init(project='my_project', group='my_group')
    model = # build model
    model = model.fit(X_train,y_train)
    y_hat = model.predict_proba(X_test)

    wandb.log(﻿{﻿"roc" : wandb.plot.roc_curve(y, y_hat﻿)﻿}﻿)

    wandb.finish()

```

Also here is a more advanced example using sweeps. Let me know if this helps to clarify or if you have any more questions about this.

Thank you,  
Nate

[Discourse post]

---

<div class="post-metadata">

**Author:** ![olto](https://avatars.discourse-cdn.com/v4/letter/o/2acd7d/32.png) [@olto](https://community.wandb.ai/u/olto)\
**Post date:** [February 15, 2022, 7:01pm UTC](https://community.wandb.ai/t/example-code-for-how-to-set-up-logging-processes-cross-validation-folds-and-grouping-them/1908/4 "2022-02-15T19:01:31Z")

</div>

Thanks a lot, Nate!

I ended up with something similar by playing around with various approaches. One thing I did differently though was calling

wandb.finish()

only once at the end of the script outside the KFold for loop. Looking at your code suggestions, I understand that I should finish every single run with wandb.finish().

Thanks a lot also for the example using sweeps. That will come in handy.

Cheers  
Oliver

---

<div class="post-metadata">

**Author:** ![nathank](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@nathank](https://community.wandb.ai/u/nathank)\
**Post date:** [February 16, 2022, 6:01pm UTC](https://community.wandb.ai/t/example-code-for-how-to-set-up-logging-processes-cross-validation-folds-and-grouping-them/1908/5 "2022-02-16T18:01:35Z")

</div>

@olto No problem! Thanks for the question. It gave me a chance to dig into this myself.

When you start a new run with `wandb.init()` it basically calls `wandb.finish()` on the previous run so both codes work essentially the same. I just like to explicitly call it to finish a run but either works!

Thank you,  
Nate

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/1X/366b649231631dbab896843020da0056074ac79d.png) [@system](https://community.wandb.ai/u/system)\
**Post date:** [April 17, 2022, 6:01pm UTC](https://community.wandb.ai/t/example-code-for-how-to-set-up-logging-processes-cross-validation-folds-and-grouping-them/1908/6 "2022-04-17T18:01:47Z")

</div>

Closed on April 17, 2022, 6:01pm UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
