如何使用if语句PyTorch使用torch.FloatTensor

时间:2017-11-14 15:55:49

标签: python gpu pytorch

我正在尝试使用 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/0.9.9/jquery.magnific-popup.min.js"></script> <script> $(document).ready(function() { $('#dnt').submit(function(e){ var cat_id = $('.dnt-overlay > select > option:selected').data('id') || 0; var cat_title = $('.dnt-overlay > select > option:selected').text() || 'All'; if (cat_id === 0) { cat_title = 'All'; } var dnt_url = $('.dnt-overlay > select > option:selected').val(); var gender = $('.dnt-overlay > [name=gender]:checked').data('id') || 0; var gender_title = $('.dnt-overlay > [name=gender]:checked').val() || 'All'; var title = cat_title; var html = ''; var params = ''; if(window.innerWidth <= 320) { params = '?limit=18'; } else { params = '?limit=21'; } if (cat_id > 0) { params += '&' + 'cat_id=' + cat_id; } if (gender > 0 && cat_title != 'Gender Agnostic' && cat_title != 'Unisex' && cat_title != 'Male' && cat_title != 'Female') { title += ' ' + gender_title; //params += '&' + 'gender=' + gender; if(cat_title == 'All') { dnt_url += gender_title.toLowerCase() + '/'; } else { dnt_url += '?gender=' + gender_title.charAt(0).toLowerCase(); } } title += ' Dog Names'; $('#dnt-popup > h1').text(title); $('#dnt-popup > .more-btn').find('a').attr('href',dnt_url); e.preventDefault(); // We don't need to send the form, because it's all local if($('#male').is(':checked')) { // Check if male is checked $('#dnt-popup').removeClass('dnt-all').removeClass('dnt-female').addClass('dnt-male'); } else if($('#female').is(':checked')) { // Check if female is checked $('#dnt-popup').removeClass('dnt-all').removeClass('dnt-male').addClass('dnt-female'); } else if($('#all').is(':checked')) { // Check if all is checked $('#dnt-popup').removeClass('dnt-female').removeClass('dnt-male').addClass('dnt-all'); } $.ajax({ dataType: "json", url: "dnt.json" }) .done(function( data ) { $.each(data, function( index, value ) { html +='<li>' + value.title + '</li>'; }); $('#dnt-popup > .results-list > ul').html(html); $.magnificPopup.open({ // open pop up for male items: { src: '#dnt-popup', type: 'inline' },//item mainClass: 'animated slideInRight' });///popup open }); });///submit function $('#dnt-popup > .more-btn > .long-btn > a, #dnt-popup > .more-btn > .short-btn > a').on("click",function(){ var cat_id = $('.dnt-overlay > select > option:selected').data('id') || 0; var cat_title = $('.dnt-overlay > select > option:selected').text() || 'All'; if (cat_id === 0) { cat_title = 'All'; } var gender_title = $('.dnt-overlay > [name=gender]:checked').val() || 'All'; }); //event for click to DNT landing page });///submit function });//JQuery </script> 作为数据类型在我的PyTorch代码中使用 if语句,以加速 GPU

这是我的代码:

    [
      {
        "Name": "Aaliyah",
        "Female": 594,
        "Baby": 601
      },
     {
        "Name": "Abby",
        "Female": 594
     },
    {
        "Name": "Abe",
        "Male": 593,
        "Video Games": 604
    },
   {
         "Name": "Abigail",
         "Female": 594,
         "Baby": 601
    },
   {
         "Name": "Abu",
         "Male": 593,
         "Disney": 598
   },
  {
         "Name": "Ace",
         "Male": 593,
         "Fancy": 600,
         "Cool": 611
   },
  {
      "Name": "Ace",
      "Male": 593,
      "Fancy": 600,
      "Cool": 611
   },
   {
      "Name": "Action",
      "Male": 593,
      "Female": 594,
      "Agnostic": 595
   },
   {
       "Name": "Ada",
       "Female": 594,
       "German": 610
    },
    {
      "Name": "Adalyn",
      "Female": 594,
      "Baby": 601
    },
  ]

不幸的是它不起作用。它给了我一个错误信息,我无法弄清楚是什么问题。

  

RuntimeError:非空的torch.cuda.ByteTensor对象的bool值不明确

如何在- releasing a cell - cell is unhighlighting - cell becomes selected 中使用 if ?我试图将cuda.Tensor转换为numpy数组,但它也不起作用。

torch.FloatTensor

PS :我是否以有效/更快的方式执行代码?或者我应该改变一些东西以获得更快的结果?

2 个答案:

答案 0 :(得分:0)

比较pyTorch张量时,输出通常为ByteTensor。此数据类型不适用于if语句。

更改if

中的条件
if (minynext[0] < miny[0])

答案 1 :(得分:0)

如果您查看以下简单示例:

import torch

a = torch.LongTensor([1])
b = torch.LongTensor([5])

print(a > b)

输出:

 0
[torch.ByteTensor of size 1]

比较张量ab会产生torch.ByteTensor,这显然不等同于boolean。所以,你可以做到以下几点。

print(a[0] > b[0]) # False

因此,您应该按照以下方式更改if条件。

if (minynext[0] < miny[0])