TypeError: Object of type int32 is not JSON serializable

username: mattpats
cooked: <p>I am training a TabNetClassifier. My code is largely borrowed from:<br>
‘<a href="https://github.com/dreamquark-ai/tabnet/blob/develop/census_example.ipynb" class="inline-onebox" rel="noopener nofollow ugc">tabnet/census_example.ipynb at develop · dreamquark-ai/tabnet · GitHub</a>’</p>
<p>Everything is working fine until I try to save the model. When trying to save the model, I get the error: ‘TypeError: Object of type int32 is not JSON serializable’</p>
<p>More details below:</p>
<pre><code class="lang-auto">from pytorch_tabnet.tab_model import TabNetClassifier

tabnet_params = {"cat_idxs": cat_idxs,
                 "cat_dims": cat_dims,
                 "cat_emb_dim": 2,
                 "optimizer_fn": torch.optim.Adam,
                 "optimizer_params": dict(lr=2e-2),
                 "scheduler_params": {"step_size": 50, "gamma": 0.9},
                 "scheduler_fn": torch.optim.lr_scheduler.StepLR,
                 "mask_type": 'entmax'}

clf = TabNetClassifier(**tabnet_params)

clf.fit(
    X_train=X_train, y_train=y_train,
    eval_set=[(X_train, y_train), (X_valid, y_valid)],
    eval_name=['train', 'valid'],
    eval_metric=['auc'],
    max_epochs=50,
    patience=20,
    batch_size=1024,
    virtual_batch_size=128,
    num_workers=0,
    weights=1,
    drop_last=False)
</code></pre>
<p>My model trains fine and I can perform inference with it. However, when I try to save it:</p>
<pre><code class="lang-auto">saving_path_name = "./tabnet_model_test_1"
saved_filepath = clf.save_model(saving_path_name)
&gt;&gt;&gt; TypeError: Object of type int32 is not JSON serializable
</code></pre>

username: rinkujadhav2013
cooked: <p>Maybe have a look here:</p><aside class="onebox githubissue" data-onebox-src="https://github.com/dreamquark-ai/tabnet/issues/424#issuecomment-1331885283">
  <header class="source">

      <a href="https://github.com/dreamquark-ai/tabnet/issues/424#issuecomment-1331885283" target="_blank" rel="noopener nofollow ugc">github.com/dreamquark-ai/tabnet</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/dreamquark-ai/tabnet/issues/424#issuecomment-1331885283" target="_blank" rel="noopener nofollow ugc">TypeError when saving a model with `numpy.bool_` types</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2022-08-01" data-time="16:08:41" data-timezone="UTC">04:08PM - 01 Aug 22 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/nishaq503" target="_blank" rel="noopener nofollow ugc">
          <img alt="nishaq503" src="https://avatars.githubusercontent.com/u/32004868?v=4" class="onebox-avatar-inline" width="20" height="20">
          nishaq503
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          bug
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">`numpy.bool_` types are not being correctly serialized to json.

**What is the<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden"> current behavior?**
The `ComplexEncoder` class ([here](https://github.com/dreamquark-ai/tabnet/blob/4fa545da50796f0d16f49d0cb476d5a30c2a27c1/pytorch_tabnet/utils.py#L336)) does not handle `numpy.bool_` which is not JSON serializable. This raises a TypeError when saving certain models.

**If the current behavior is a bug, please provide the steps to reproduce.**
```python
model = TabNetClassifier(...)
model.fit(...)  # training data and model parameters contain values of type numpy.bool_
model.save_model('path/to/model')
```

**Expected behavior**
`numpy.bool_` should be cast to python's `bool` before being serialized to JSON. Here is my suggested fix. Please let me know if this is acceptable for a PR:

```python
class ComplexEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.int64):
            return int(obj)
        if isinstance(obj, np.bool_):
            return bool(obj)
        # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)
```

**Other relevant information:**
poetry version:  "poetry-core&gt;=1.0.0"
python version: "^3.9"
Operating System: "Linux Kernel 5.18.14-arch1-1"
Additional tools:
CUDA Version: 11.7
Driver Version: 515.57

**Additional context**

Here's a stacktrace:
```bash
  File ".venv/lib/python3.10/site-packages/pytorch_tabnet/abstract_model.py", line 375, in save_model
    json.dump(saved_params, f, cls=ComplexEncoder)
  File "/usr/lib/python3.10/json/__init__.py", line 179, in dump
    for chunk in iterable:
  File "/usr/lib/python3.10/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.10/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.10/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File ".venv/lib/python3.10/site-packages/pytorch_tabnet/utils.py", line 339, in default
    return json.JSONEncoder.default(self, obj)
  File "/usr/lib/python3.10/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bool_ is not JSON serializable
```

I ran into this when trying tabnet in a [kaggle competition](https://www.kaggle.com/competitions/amex-default-prediction). If you need to, you can look [here in my code](https://github.com/nishaq503/amex/blob/b4dabc441581a6f29318fcbd2a83ec5376bc1f5a/amex/models/tabnet/training.py#L121) where the error happens.</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
and here:</p><aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/49795525/typeerror-object-of-type-int32-is-not-json-serializable/49797251#49797251">
  <header class="source">

      <a href="https://stackoverflow.com/questions/49795525/typeerror-object-of-type-int32-is-not-json-serializable/49797251#49797251" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/2091925/will-keeling" target="_blank" rel="noopener nofollow ugc">
    <img alt="Will Keeling" src="https://www.gravatar.com/avatar/2cdc96737b990c24e08de6174eed0b0f?s=256&amp;d=identicon&amp;r=PG" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/49795525/typeerror-object-of-type-int32-is-not-json-serializable/49797251#49797251" target="_blank" rel="noopener nofollow ugc">TypeError: Object of type 'int32' is not JSON serializable</a>
</h4>

<div class="tags">
  <strong>python, ajax, django, django-views</strong>
</div>

<div class="date">
  
  answered by
  <a href="https://stackoverflow.com/users/2091925/will-keeling" target="_blank" rel="noopener nofollow ugc">
    Will Keeling
  </a>
  on <a href="https://stackoverflow.com/questions/49795525/typeerror-object-of-type-int32-is-not-json-serializable/49797251#49797251" target="_blank" rel="noopener nofollow ugc">12:58PM - 12 Apr 18 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>