pythonnumpypytorchtensorboard

AttributeError: `np.string_` was removed in the NumPy 2.0 release. Use `np.bytes_` instead.. Did you mean: 'strings'?


I m interested in seeing neural network as graph using tensorboard. I have constructed a network in pytorch with following code-

import torch
BATCH_SIZE = 16
DIM_IN = 1000
HIDDEN_SIZE = 100
DIM_OUT = 10

class TinyModel(torch.nn.Module):

    def __init__(self):
        super(TinyModel, self).__init__()

        self.layer1 = torch.nn.Linear(DIM_IN, HIDDEN_SIZE)
        self.relu = torch.nn.ReLU()
        self.layer2 = torch.nn.Linear(HIDDEN_SIZE, DIM_OUT)

    def forward(self, x):
        x = self.layer1(x)
        x = self.relu(x)
        x = self.layer2(x)
        return x

some_input = torch.randn(BATCH_SIZE, DIM_IN, requires_grad=False)
ideal_output = torch.randn(BATCH_SIZE, DIM_OUT, requires_grad=False)

model = TinyModel()

Setting-up tensorboard

from torch.utils.tensorboard import SummaryWriter

# Create a SummaryWriter
writer = SummaryWriter("checkpoint")

# Add the graph to TensorBoard
writer.add_graph(model, some_input)
writer.close()

While I run tensorboard --logdir=checkpoint on terminal , I receive the following error -

Traceback (most recent call last):
  File "/home/k/python_venv/bin/tensorboard", line 5, in <module>
    from tensorboard.main import run_main
  File "/home/k/python_venv/lib/python3.10/site-packages/tensorboard/main.py", line 27, in <module>
    from tensorboard import default
  File "/home/k/python_venv/lib/python3.10/site-packages/tensorboard/default.py", line 39, in <module>
    from tensorboard.plugins.hparams import hparams_plugin
  File "/home/k/python_venv/lib/python3.10/site-packages/tensorboard/plugins/hparams/hparams_plugin.py", line 30, in <module>
    from tensorboard.plugins.hparams import backend_context
  File "/home/k/python_venv/lib/python3.10/site-packages/tensorboard/plugins/hparams/backend_context.py", line 26, in <module>
    from tensorboard.plugins.hparams import metadata
  File "/home/k/python_venv/lib/python3.10/site-packages/tensorboard/plugins/hparams/metadata.py", line 32, in <module>
    NULL_TENSOR = tensor_util.make_tensor_proto(
  File "/home/k/python_venv/lib/python3.10/site-packages/tensorboard/util/tensor_util.py", line 405, in make_tensor_proto
    numpy_dtype = dtypes.as_dtype(nparray.dtype)
  File "/home/k/python_venv/lib/python3.10/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py", line 677, in as_dtype
    if type_value.type == np.string_ or type_value.type == np.unicode_:
  File "/home/k/python_venv/lib/python3.10/site-packages/numpy/__init__.py", line 397, in __getattr__
    raise AttributeError(
AttributeError: `np.string_` was removed in the NumPy 2.0 release. Use `np.bytes_` instead.. Did you mean: 'strings'? 

Probably the issue will be fixed in future releases, but is there a fix for now?


Solution

  • Your code seems to be written in numpy < 2.0 compatible way, but it looks like you are running it with numpy >=2. Try downgrading the numpy version to < 2.0.