RuntimeError:给定组= 1,权重为[64,3,7,7],预期输入[3,1,224,224]具有3个通道,但改为1个通道

时间:2018-11-21 16:44:52

标签: python deep-learning pytorch tensor

在下面的代码中:

var localCache = {
    data: {},
    remove: function (url) {
        delete localCache.data[url];
    },
    exist: function (url) {
        return localCache.data.hasOwnProperty(url) && localCache.data[url] !== null;
    },
    get: function (url) {
         console.debug('fatching data from URL ', url);
        return localCache.data[url];
    },
    set: function (url, check, cachedData, callback) {
         if(this.ignore(check)){
            console.debug('Need To Ignore URL ', check);
        }else{
            localCache.remove(url);
            localCache.data[url] = cachedData;
             console.debug('caching data from URL ', url);
            if ($.isFunction(callback)) callback(cachedData);
        }
    },
    ignore: function(url){
      var Arrays = [
          'cart.php',
          'favorites.php',
          'view_item.php'
      ];
      return ($.inArray( url, Arrays ) !== -1);
    }
};

我收到以下错误:

    model_ft.eval()
    test_data, test_target = image_datasets['train'][idx]
    test_data = test_data.cuda()
    #test_target = test_target.cuda()
    test_target = torch.tensor(test_target)
    test_target = test_target.cuda()
    test_data.unsqueeze_(1)
    test_target.unsqueeze_(0)
    print(test_data.shape)
    output = model_ft(test_data)

test_data的形状也为:torch.Size([3,1,224,224])。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

解决方法是

test_data, test_target = image_datasets['train'][idx]
test_data = test_data.cuda()
test_target = torch.tensor(test_target)
test_target = test_target.cuda()
test_data.unsqueeze_(0)
test_target.unsqueeze_(0)
output = model_ft(test_data)

我必须将test_data.unsqueeze_(1)更改为test_data.unsqueeze_(0)