question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

example/mnist/train_mnist.py fails when using GPU

See original GitHub issue

When I executed chainer/chainer/example/mnist/train_mnist.py with GPU, the following error occurred. It seems that it uses both of numpy.ndarray and cupy.ndarray.

I used the latest official docker container GPU : TITAN-X

root@ce05d3cc6624:~/work/chainer/examples/mnist# ./train_mnist.py --gpu=0
GPU: 0
# unit: 1000
# Minibatch-size: 100
# epoch: 20

/usr/local/lib/python2.7/dist-packages/chainer/training/extensions/plot_report.py:24: UserWarning: matplotlib is not installed on your environment, so nothing will be plotted at this time. Please install matplotlib to plot figures.

  $ pip install matplotlib

  warnings.warn('matplotlib is not installed on your environment, '
Traceback (most recent call last):
  File "./train_mnist.py", line 137, in <module>
    main()
  File "./train_mnist.py", line 133, in main
    trainer.run()
  File "/usr/local/lib/python2.7/dist-packages/chainer/training/trainer.py", line 295, in run
    update()
  File "/usr/local/lib/python2.7/dist-packages/chainer/training/updater.py", line 177, in update
    self.update_core()
  File "/usr/local/lib/python2.7/dist-packages/chainer/training/updater.py", line 189, in update_core
    optimizer.update(loss_func, *in_vars)
  File "/usr/local/lib/python2.7/dist-packages/chainer/optimizer.py", line 411, in update
    loss = lossfun(*args, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/chainer/links/model/classifier.py", line 67, in __call__
    self.y = self.predictor(*x)
  File "./train_mnist.py", line 39, in __call__
    h1 = F.relu(self.l1(x))
  File "/usr/local/lib/python2.7/dist-packages/chainer/links/connection/linear.py", line 93, in __call__
    return linear.linear(x, self.W, self.b)
  File "/usr/local/lib/python2.7/dist-packages/chainer/functions/connection/linear.py", line 107, in linear
    return LinearFunction()(x, W, b)
  File "/usr/local/lib/python2.7/dist-packages/chainer/function.py", line 199, in __call__
    outputs = self.forward(in_data)
  File "/usr/local/lib/python2.7/dist-packages/chainer/functions/connection/linear.py", line 40, in forward
    .format(type(W), type(x)))
ValueError: numpy and cupy must not be used together
type(W): <type 'numpy.ndarray'>, type(x): <type 'cupy.core.core.ndarray'>
root@ce05d3cc6624:~/work/chainer/examples/mnist#

Another error said that there is not self.init_scope(), so I modified the MLP class as following:

# Network definition
class MLP(chainer.Chain):

    # def __init__(self, n_units, n_out):
    #     super(MLP, self).__init__()
    #     with self.init_scope():
    #         # the size of the inputs to each layer will be inferred
    #         self.l1 = L.Linear(None, n_units)  # n_in -> n_units
    #         self.l2 = L.Linear(None, n_units)  # n_units -> n_units
    #         self.l3 = L.Linear(None, n_out)  # n_units -> n_out

    def __init__(self, n_units, n_out):
        super(MLP, self).__init__()
        # the size of the inputs to each layer will be inferred
        self.l1 = L.Linear(None, n_units)  # n_in -> n_units
        self.l2 = L.Linear(None, n_units)  # n_units -> n_units
        self.l3 = L.Linear(None, n_out)  # n_units -> n_out

    def __call__(self, x):
        h1 = F.relu(self.l1(x))
        h2 = F.relu(self.l2(h1))
        return self.l3(h2)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
DenDen047commented, Jun 2, 2017

I could run python train_mnist.py. But I couldn’t run `python train_mnist.py --gpu=0’, and the following error occurred.

root@ce05d3cc6624:~/work/chainer/examples/mnist# ./train_mnist.py --gpu=0
GPU: 0
# unit: 1000
# Minibatch-size: 100
# epoch: 20

/usr/local/lib/python2.7/dist-packages/chainer/training/extensions/plot_report.py:24: UserWarning: matplotlib is not installed on your environment, so nothing will be plotted at this time. Please install matplotlib to plot figures.

  $ pip install matplotlib

  warnings.warn('matplotlib is not installed on your environment, '
Traceback (most recent call last):
  File "./train_mnist.py", line 130, in <module>
    main()
  File "./train_mnist.py", line 126, in main
    trainer.run()
  File "/usr/local/lib/python2.7/dist-packages/chainer/training/trainer.py", line 295, in run
    update()
  File "/usr/local/lib/python2.7/dist-packages/chainer/training/updater.py", line 177, in update
    self.update_core()
  File "/usr/local/lib/python2.7/dist-packages/chainer/training/updater.py", line 189, in update_core
    optimizer.update(loss_func, *in_vars)
  File "/usr/local/lib/python2.7/dist-packages/chainer/optimizer.py", line 411, in update
    loss = lossfun(*args, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/chainer/links/model/classifier.py", line 67, in __call__
    self.y = self.predictor(*x)
  File "./train_mnist.py", line 32, in __call__
    h1 = F.relu(self.l1(x))
  File "/usr/local/lib/python2.7/dist-packages/chainer/links/connection/linear.py", line 92, in __call__
    self._initialize_params(x.size // x.shape[0])
  File "/usr/local/lib/python2.7/dist-packages/chainer/links/connection/linear.py", line 78, in _initialize_params
    initializer=self._W_initializer)
  File "/usr/local/lib/python2.7/dist-packages/chainer/link.py", line 179, in add_param
    data = initializers.generate_array(initializer, shape, self.xp)
  File "/usr/local/lib/python2.7/dist-packages/chainer/initializers/__init__.py", line 46, in generate_array
    initializer(array)
  File "/usr/local/lib/python2.7/dist-packages/chainer/initializers/normal.py", line 99, in __call__
    Normal(s)(array)
  File "/usr/local/lib/python2.7/dist-packages/chainer/initializers/normal.py", line 36, in __call__
    array[...] = xp.random.normal(**args)
  File "/usr/local/lib/python2.7/dist-packages/cupy/random/distributions.py", line 90, in normal
    return (rs.normal(0, 1, size, dtype) * scale + loc).astype(dtype)
  File "/usr/local/lib/python2.7/dist-packages/cupy/random/generator.py", line 105, in normal
    return self._generate_normal(func, size, dtype, loc, scale)
  File "/usr/local/lib/python2.7/dist-packages/cupy/random/generator.py", line 68, in _generate_normal
    func(self._generator, out.data.ptr, out.size, *args)
  File "cupy/cuda/curand.pyx", line 171, in cupy.cuda.curand.generateNormalDouble (cupy/cuda/curand.cpp:3431)
  File "cupy/cuda/curand.pyx", line 179, in cupy.cuda.curand.generateNormalDouble (cupy/cuda/curand.cpp:3313)
  File "cupy/cuda/curand.pyx", line 85, in cupy.cuda.curand.check_status (cupy/cuda/curand.cpp:1438)
cupy.cuda.curand.CURANDError: CURAND_STATUS_LAUNCH_FAILURE
Read more comments on GitHub >

github_iconTop Results From Across the Web

GPU memory error with train.py and eval.py running together
Evaluation fails with an error about the GPU being out of memory (training continues though in the other terminal window with no problem)....
Read more >
Using GPU error when use TensorFlow to train image
It seems that you Tensorflow is not detecting any gpu as available but maps the operations to GPU:0. First try this:
Read more >
Python, Performance, and GPUs - Towards Data Science
Probably the easiest way for a Python programmer to get access to GPU performance is to use a GPU-accelerated Python library.
Read more >
vGPU Enabled UVMs May be Unable to be Created, or May ...
Summary: The vGPU option may appear greyed out and unavailable when using Nvidia GRID with certain Tesla GPUs if the GPU is configured...
Read more >
Tensorflow Plugin - Metal - Apple Developer
Mac computers with Apple silicon or AMD GPUs · macOS 12.0 or later (Get the latest beta) · Python 3.8 or later ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found