AttributeError: 'numpy.float32' object has no attribute 'to_cpu'

339 views
Skip to first unread message

msabda...@gmail.com

unread,
Sep 17, 2019, 9:34:21 PM9/17/19
to Chainer User Group
Hi all,

Could you please help me with this error? Below is the snipestof the code from which the error originates.

def get_idp_acc(model, dataset_tuple, comp_ratio, profile = None, batchsize = 128, gpu = 0):
    chainer
.config.train = True
   
import numpy as np
   
#xp = np if gpu < 0 else cuda.cupy
    x
, indices, x_zf, HtH, Hty = dataset_tuple._datasets[0], dataset_tuple._datasets[1], dataset_tuple._datasets[2], dataset_tuple._datasets[3], dataset_tuple._datasets[4]
   
    accs
= 0
   
BERs = 0
    model
.train = False

   
for j in range(0, len(x), batchsize):
        x_batch
= np.array(x[j:j + batchsize])
        indices_batch
= np.array(indices[j:j + batchsize])
        x_zf_batch
= np.array(x_zf[j:j + batchsize])
       
HtH_batch = np.array(HtH[j:j + batchsize])
       
Hty_batch = np.array(Hty[j:j + batchsize])
       
if profile == None:
            acc_data
= model(x_batch, indices_batch, x_zf_batch, HtH_batch, Hty_batch, comp_ratio=comp_ratio,
                            ret_param
= 'acc')
       
else:
            acc_data
= model(x_batch, indices_batch, x_zf_batch, HtH_batch, Hty_batch, comp_ratio=comp_ratio,
                            ret_param
='acc', profile=profile)
        acc_data
.to_cpu()
        acc
= acc_data.data
        BER
= 1 - acc
        accs
+= acc * len(x_batch)
       
BERs += BER * len(x_batch)
   
return (accs / len(x)) * 100.

The error is as shown below.

Traceback (most recent call last):
 
File "/Users/mac/Documents/idp_detnet/examples/run_mlp.py", line 14, in <module>
    mlp
.run(args)
 
File "/Users/mac/Documents/idp_detnet/examples/mlp.py", line 39, in run
    acc_dict
[name], BER_dict[name] = util.sweep_idp(model, test, comp_ratios, args)
 
File "/Users/mac/Documents/idp_detnet/examples/util.py", line 109, in sweep_idp
    batchsize
=args.batchsize, profile=profile))
 
File "/Users/mac/Documents/idp_detnet/examples/util.py", line 85, in get_idp_acc
    acc_data
.to_cpu()
AttributeError: 'numpy.float32' object has no attribute 'to_cpu'



Thank you in anticipation of your kind response.

Abdullahi Mohammad

Kenichi Maehashi

unread,
Sep 30, 2019, 1:38:58 AM9/30/19
to Chainer User Group
Reply all
Reply to author
Forward
0 new messages