Chains fail to learn due to errors in learning

Asked 2 years ago, Updated 2 years ago, 95 views

It's my first time using it.
I study machine learning using a chain called Convolutional LSTM.

In an experiment using MovingMnist, the program worked, but when I tried to use the data I had, I got an error and I am in trouble.
The input data will be 320x256 bmp data (one channel).

Error Contents

/home/denko/anaconda3/lib/python3.6/site-packages/h5py/_init__.py:36: FutureWarning: Conversion of the second argument of issudtype from `float` to `np.floating` is decremented.Infuture, will be flat.naped type(64)
  from._conv import register_converters as_register_converters

Exception in main training loop: Unsupported dtype object
Traceback (most recent call last):
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/training/trainer.py", line 299, in run
    update()
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/training/updater.py", line 223, in update
    self.update_core()
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/training/updater.py", line 228, in update_core
    in_arrays=self.converter(batch,self.device)
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/dataset/convert.py", line 93, in concat_examples
    [example[i] for example in batch], padding[i])))
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/dataset/convert.py", line 35, into_device
    return cuda.to_gpu(x,device)
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/backends/cuda.py", line 275, into_gpu
    return_array_to_gpu(array,device_,stream)
  File"/home/denko/anaconda3/lib/python3.6/site-packages/chainer/backends/cuda.py", line322, in_array_to_gpu
    return cupy.asarray(array)
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/cupy/creation/from_data.py", line61, in asarray
    return core.array(a,dtype,false)
  File "cupy/core/core.pyx", line 2070, incupy.core.core.array
  File "cupy/core/core.pyx", line 2101, incupy.core.core.array
Will finalize trainer extensions and updater before re-raising the exception.
Traceback (most recent call last):
  File "train.py", line 81, in<module>
    train()
  File "train.py", line 69, intrain
    trainer.run()
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/training/trainer.py", line 313, in run
    Six.release (*sys.exc_info())
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/six.py", line 693, inrelease
    raise value
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/training/trainer.py", line 299, in run
    update()
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/training/updater.py", line 223, in update
    self.update_core()
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/training/updater.py", line 228, in update_core
    in_arrays=self.converter(batch,self.device)
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/dataset/convert.py", line 93, in concat_examples
    [example[i] for example in batch], padding[i])))
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/dataset/convert.py", line 35, into_device
    return cuda.to_gpu(x,device)
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/chainer/backends/cuda.py", line 275, into_gpu
    return_array_to_gpu(array,device_,stream)
  File"/home/denko/anaconda3/lib/python3.6/site-packages/chainer/backends/cuda.py", line322, in_array_to_gpu
    return cupy.asarray(array)
  File"/home/denko/anaconda3/lib/python 3.6/site-packages/cupy/creation/from_data.py", line61, in asarray
    return core.array(a,dtype,false)
  File "cupy/core/core.pyx", line 2070, incupy.core.core.array
  File "cupy/core/core.pyx", line 2101, incupy.core.core.array
ValueError: Unsupported dtype object

code

--dataset_for_rr --

import numpy as np
import chain
from PIL import Image
from chain import cuda, Variable

classrrDataset(chain.dataset.DatasetMixin):
    def_init__(self, l, r, inf, outf):
        self.rr_list=np.load('/media/denko/OS/data/rr_list/test.npy')
        self.l=l
        self.r = r
        self.inf = inf
        self.outf = outf
        self.frames = self.inf + self.outf
        self.num = self.r-self.l
        self.xp=cuda.cupy

    def__len__(self):
        return self.num

    def get_example(self, i):
        self.ind=self.l+i
        self.data=np.empty(self.frames, 1,256,320), dtype=np.float32)

        for jin range (self.frames):
            self.bmp_name = str(int(self.rr_list[self.ind][j]))
            self.year=self.bmp_name [0:4]
            self.month=self.bmp_name [4:6]
            self.path='/media/denko/OS/data/bmp/'+self.year+'/'+self.month+'/'+self.bmp_name+'.bmp'
            self.bmp_data=Image.open(self.path)
            self.bmp_data=np.array(self.bmp_data).astype(dtype=np.float32)
            self.bmp_data =self.bmp_data/255
            self.data [j, 0, :, :] = self.bmp_data

        self.data=Variable(np.array(self.data,dtype=np.float32))

        return self.data [:self.inf, 0, :, :], self.data [self.inf:self.frames, 0, :, :]

python chainer

2022-09-30 21:33

1 Answers

In short,

34self.data=Variable(np.array(self.data,dtype=np.float32))

34self.data=np.array(self.data,dtype=np.float32)

It would be all right if

The details are as follows.
It was hard to understand, so I added a line.

1 import numpy as np
  2 import chain
  3 from PIL import Image
  4 from chain import cuda, Variable
  5
  6 class rrDataset(chain.dataset.DatasetMixin):
  7def_init__(self, l, r, inf, outf):
  8self.rr_list=np.load('/media/denko/OS/data/rr_list/test.npy')
  9self.l=l
 10self.r=r
 11self.inf = inf
 12self.outf=outf
 13 self.frames = self.inf + self.outf
 14 self.num = self.r-self.l
 15self.xp=cuda.cupy
 16
 17 def__len__(self):
 18 return self.num
 19
 20def get_example(self, i):
 21 self.ind=self.l+i
 22 self.data=np.empty(self.frames, 1,256,320), dtype=np.float32)
 23
 24 for jin range (self.frames):
 25self.bmp_name = str(int(self.rr_list[self.ind][j]))
 26 self.year=self.bmp_name [0:4]
 27 self.month=self.bmp_name [4:6]
 28self.path='/media/denko/OS/data/bmp/'+self.year+'/'+self.month+'/'+self.bmp_name+'.bmp'
 29self.bmp_data=Image.open(self.path)
 30self.bmp_data=np.array(self.bmp_data).astype(dtype=np.float32)
 31self.bmp_data =self.bmp_data/255
 32 self.data [j, 0, :, :] = self.bmp_data
 33
 34 self.data=Variable(np.array(self.data,dtype=np.float32))
 35
 36 return self.data [:self.inf, 0, :, :], self.data [:self.inf:self.frames, 0, :, :]

The problem is that, as Metropolis points out, we are returning Variable in line 34. DatasetMixin is usually expected to return np.ndarray or np.ndarray tuples.

Here is a short code for verification.

 1 from chain.dataset.convert import concat_examples
  2 from chain import Variable
  3 import numpy as np
  4
  5 data_np_1 = np.range (9, dtype=np.float32).reshape ((3,3))
  6 data_np_2 = data_np_1*2
  7 data_Variable_1 = Variable(data_np_1)
  8 data_Variable_2 = Variable(data_np_2)
  9
 10batch_1_np=(data_np_1[:1,:], data_np_1[1:,:])
 11batch_2_np=(data_np_2[:1,:], data_np_2[1:,:])
 12
 13batch_1_Variable=(data_Variable_1[:1,:], data_Variable_1[1:,:])
 14batch_2_Variable=(data_Variable_2[:1,:], data_Variable_2[1:,:])
 15
 16 print(concat_examples([batch_1_np,batch_2_np]))
 17 print(concat_examples([batch_1_Variable, batch_2_Variable]))
 18

The results are as follows.

(
  array([
          [[0., 1., 2.]],
          [[0., 2., 4.]]
        , dtype=float32),
  array([
          [[ 3.,  4.,  5.], [ 6.,  7.,  8.]],
          [[ 6.,  8., 10.], [12., 14., 16.]]
        , dtype=float32)
)

(
  array([
          [[variable(0.), variable(1.), variable(2.)],
          [[variable(0.), variable(2.), variable(4.)]]
        , dtype=object),
  array([
          [[variable(3.), variable(4.), variable(5.), variable(6.), variable(7.), variable(8.)],
          [[variable(6.), variable(8.), variable(10.), variable(12.), variable(14.), variable(16.)]]]
        , dtype = object )
)

In this way, ndarray with dtype of object is generated, so
ValueError: Unsupported dtype object
That's what they say.

In chain.dataset.convert._concat_arrays,

if not instance(arrays[0], numpy.ndarray) and \
   not isinstance(arrays[0], cuda.ndarray):
    arrays=numpy.asarray(arrays)

is called, and if you give variable to numpy.asarray, the return value will not be the normal ndarray.

Below are Tips, so you can skip them.

inf is a reserved word for Infinity in math. It is recommended not to use it.
Similarly, the .data attribute is a reservation word for pointer to memory with numpy and cupy, and xp.ndarray with Chainer, so it is recommended not to use it yourself. (Actually, the reservation word has been removed from v3.0.0 or later, because it avoids conflict with numpy and cupy.)


2022-09-30 21:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.