TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first

对着背影说爱祢 提交于 2021-01-29 07:14:40

问题


I am running a model on Google Colab.As I reproduce the code,I want to run one of the pieces of code to get the experimental results.And I get some error.Here is the code:

cluster_args = {
    'cluster_layers' : {1:400, 3:240},
    'conv_feature_size' : 1,
    'reshape_exists' : False,
    'features' : 'both',
    'channel_reduction' : 'fro',
    'use_bias' : False,
    'linkage_method' : 'ward',
    'distance_metric' : 'euclidean',
    'cluster_criterion' : 'hierarchical_trunc',
    'distance_threshold' : 1.60,
    'merge_criterion' : 'max_l2_norm',    
    'verbose' : False
}

path = args.checkpoint_path[:-4] + '_small_cup.pth' 
model_modifier = cluster_model(ann,cluster_args)
compressed_model = model_modifier.cluster_model()
compressed_model.cuda()

val_loss_no_retrain, val_accuracy_no_retrain = test(args, compressed_model, device, test_loader,verbose=False)

args.lr = 0.1
args.epochs = 30
optimizer = optim.SGD(compressed_model.parameters(),lr=args.lr,momentum=args.momentum,weight_decay=args.weight_decay,nesterov=False)

best_val_accuracy_retrain = 0

if not os.path.isfile(path):    
    for epoch in range(1, args.epochs+1):
        adjust_learning_rate(args,optimizer,epoch)
        train(args, compressed_model, device, train_loader, optimizer, epoch)
        val_loss_retrain, val_accuracy_retrain = test(args, compressed_model, device, test_loader)

        if val_accuracy_retrain > best_val_accuracy_retrain:  
            torch.save(compressed_model, path, pickle_protocol=4)            
            best_val_accuracy_retrain = val_accuracy_retrain   
else:
    compressed_model = torch.load(path)
    val_loss_retrain, val_accuracy_retrain = test(args, compressed_model, device, test_loader, verbose=False)
    best_val_accuracy_retrain = val_accuracy_retrain

print('Accuracy post pruning : {} (without retraining), {} (with retraining)'.format(val_accuracy_no_retrain,best_val_accuracy_retrain))

I am getting this error when I run this cell:

TypeError                                 Traceback (most recent call last)
<ipython-input-17-c3d69464b0c0> in <module>()
     16 path = args.checkpoint_path[:-4] + '_small_cup.pth'
     17 model_modifier = cluster_model(ann,cluster_args)
---> 18 compressed_model = model_modifier.cluster_model()
     19 compressed_model.cuda()
     20 

5 frames
/usr/local/lib/python3.6/dist-packages/torch/tensor.py in __array__(self, dtype)
    484     def __array__(self, dtype=None):
    485         if dtype is None:
--> 486             return self.numpy()
    487         else:
    488             return self.numpy().astype(dtype, copy=False)

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

**I try to change **

compressed_model = model_modifier.cluster_model()

to

compressed_model = model_modifier.cluster_model().data().cpu().numpy()

but still get the same error

How do I solve this?

[enter link description here][1]

来源:https://stackoverflow.com/questions/61028705/typeerror-cant-convert-cuda-tensor-to-numpy-use-tensor-cpu-to-copy-the-tens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!