Error in extraction tensor

randino: <p>I want to extract the cordonne, the code is</p>
<pre><code class="lang-auto">while (k!=3):
 j=0;
 i=0; 
 while((i!=20) and (j!=20) and 
       (((x[0,0])!=(mat_diff['img1taille500'][i,j])) and (x[0,1]!=(mat_diff['img1taille500'][i,j+1])) 
      and(x[0,2]!=mat_diff['img1taille500'][i,j+2])and (x[0,3]!=mat_diff['img1taille500'][i,j+3])
      and(x[0,4]!=mat_diff['img1taille500'][i,j+4])and(x[1,0] != mat_diff['img1taille500'][i+1,j]) 
    and(x[1,1]!=mat_diff['img1taille500'][i+1,j+1]) and (x[1,2]!=mat_diff['img1taille500'][i+1,j+2]) 
    and(x[1,3]!=mat_diff['img1taille500'][i+1,j+3])and (x[1,4]!=mat_diff['img1taille500'][i+1,j+4])
 and(x[2,0] != mat_diff['img1taille500'][i+2,j]) and (x[2,1]!=mat_diff['img1taille500'][i+2,j+1])
  and(x[2,2]!=mat_diff['img1taille500'][i+2,j+2])and (x[2,3]!=mat_diff['img1taille500'][i+2,j+3])
and(x[2,4]!=mat_diff['img1taille500'][i+2,j+4])and (x[3,0] != mat_diff['img1taille500'][i+3,j])
and(x[3,1]!=mat_diff['img1taille500'][i+3,j+1]) and (x[3,2]!=mat_diff['img1taille500'][i+3,j+2])
and(x[3,3]!=mat_diff['img1taille500'][i+3,j+3])and (x[3,4]!=mat_diff['img1taille500'][i+3,j+4]) 
and(x[4,0] !=mat_diff['img1taille500'][i+4,j]) and (x[4,1]!=mat_diff['img1taille500'][i+4,j+1]) 
and(x[4,2]!=mat_diff['img1taille500'][i+4,j+2])and (x[4,3]!=mat_diff['img1taille500'][i+4,j+3])
 and(x[4,4]!=mat_diff['img1taille500'][i+4,j+4]))):
   j=j+1; 

 if (((x[0,0])==(mat_diff['img1taille500'][i,j])) and (x[0,1]==mat_diff['img1taille500'][i,j+1]) 
      and(x[0,2]==mat_diff['img1taille500'][i,j+2])and (x[0,3]==mat_diff['img1taille500'][i,j+3])
      and(x[0,4]==mat_diff['img1taille500'][i,j+4])and(x[1,0] == mat_diff['img1taille500'][i+1,j]) 
    and(x[1,1]==mat_diff['img1taille500'][i+1,j+1]) and (x[1,2]==mat_diff['img1taille500'][i+1,j+2]) 
    and(x[1,3]==mat_diff['img1taille500'][i+1,j+3])and (x[1,4]==mat_diff['img1taille500'][i+1,j+4])
 and(x[2,0] == mat_diff['img1taille500'][i+2,j]) and (x[2,1]==mat_diff['img1taille500'][i+2,j+1])
  and(x[2,2]==mat_diff['img1taille500'][i+2,j+2])and (x[2,3]==mat_diff['img1taille500'][i+2,j+3])
and(x[2,4]==mat_diff['img1taille500'][i+2,j+4])and (x[3,0] == mat_diff['img1taille500'][i+3,j])
and(x[3,1]==mat_diff['img1taille500'][i+3,j+1]) and (x[3,2]==mat_diff['img1taille500'][i+3,j+2])
and(x[3,3]==mat_diff['img1taille500'][i+3,j+3])and (x[3,4]==mat_diff['img1taille500'][i+3,j+4]) 
and(x[4,0] ==mat_diff['img1taille500'][i+4,j]) and (x[4,1]==mat_diff['img1taille500'][i+4,j+1]) 
and(x[4,2]==mat_diff['img1taille500'][i+4,j+2])and (x[4,3]==mat_diff['img1taille500'][i+4,j+3])
and(x[4,4]==mat_diff['img1taille500'][i+4,j+4])):                  
          x1=i+2;
          y1=j+2;
          #print("les axes",x1,y1)
          abscice= x1;
          ordonne=y1;
          print(abscice,ordonne)
          #c=c+1;
          #print("nombre:",c)
          k=k+1; 
          x=train_x[k];
          i=0;
          j=0;  
 if (i==(20)):
          i=0;
          j=j+1;
 else :
          j=0;
          i=i+1; 

</code></pre>
<p>RuntimeError: Boolean value of Tensor with more than one value is ambiguous</p>

ptrblck: <p>It seems one or more of your conditions is trying to use multiple values, which is wrong and will raise the error. Here is a small code snippet showing this behavior:</p>
<pre><code class="lang-python">a = torch.tensor([True])
if a: # works
    print('works')
    
b = torch.tensor([True, False])
if b: # fails
    print('fails')
# &gt; RuntimeError: Boolean value of Tensor with more than one value is ambiguous
</code></pre>
<p>so you would have to check all conditions and make sure they return a valid single <code>bool</code> value.</p>

Eta_C: <p>On the other hand, your condition equals to …</p>
<pre><code class="lang-python">while (k != 3):
    j = 0
    i = 0
    while i != 20 and j != 20 and (x[:5, :5] != mat_diff["img1taille500"][i:i+5, j:j+5]).all():
        j += 1 
    if (x[:5, :5] == mat_diff["img1taille500"][i:i+5, j:j+5]).all():
        x1 = i + 2
        y1 = j + 2 
        ...
</code></pre>

randino: <p>hi <a class="mention" href="/u/eta_c">@Eta_C</a> <a class="mention" href="/u/ptrblck">@ptrblck</a>  thanks for this your reply, but I have other problem</p>
<pre><code class="lang-auto">    train_x=patches_diff
    train_y=labels
    patches_diff = patches_diff.reshape(patches_diff.shape[0], patches_diff.shape[1] * patches_diff.shape[2])
    train_x, val_x, train_y, val_y = train_test_split(train_x, train_y, test_size=0.4)
    train_x = train_x.reshape(train_x.shape[0], 1, train_x.shape[1], train_x.shape[2])  
    train_x = torch.from_numpy(train_x)
    print('train',train_x)
    mat_fname_diff = pjoin('./sample_data/', 'img1taille500.mat')
    #print('Mat loading ...')
    mat_diff = sio.loadmat(mat_fname_diff)
    print(mat_diff)
    abscice=[];
    cordonne=[];
    k=0;
    c=0;
    i=0;
    j=0;
    k1=0;
    abscice=[];
    ordonnee=[];
    x=train_x[k];
    k1=0
    while (k!=(4)):
      while((i!=20) and (j!=20) and (x[:5, :5] != mat_diff[i:i+5, j:j+5])):
        j=j+1;

          # for j in range (95):

      if (x[:5, :5] == mat_diff[i:i+5, j:j+5]):
          x1= i+2;
          y1=j+2;
          #print("les axes",x1,y1)
          abscice[k]= x1;
          ordonne[k]=y1;
          #print(abscice,ordonne)
          #c=c+1;
          #print("nombre:",c)
          k=k+1; 
          x=patches_diff[k];
          j=0;
          i=0;       
      elif (i==(20)):
          i=0;
          j=j+1;
      else :
          j=0;
          i=i+1;
   
</code></pre>
<p>TypeError: unhashable type: ‘slice’<br>
help me plzz</p>

randino: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/eta_c">@Eta_C</a>  I modified with this code, but this code executes but returns no results</p>
<pre><code class="lang-auto"> mat_fname_diff = pjoin('./sample_data/', 'img1taille500.mat')
    #print('Mat loading ...')
    mat_diff = sio.loadmat(mat_fname_diff)
    patches_diff = image.extract_patches_2d(mat_diff['img1taille500'], ((5,5)))
    print(mat_diff)
    abscice=[];
    cordonne=[];
    k=0;
    c=0;
    i=0;
    j=0;
    k1=0;
    abscice=[];
    ordonnee=[];
    x=train_x[k];
    k1=0
    while (k!=(4)):
      while((i!=20) and (j!=20) and (x[:5, :5] != (mat_diff['img1taille500'][i:i+5, j:j+5]))):
        j=j+1;
          # for j in range (95):

      if (x[:5, :5] == (mat_diff['img1taille500'][i:i+5, j:j+5])):
          x1= i+2;
          y1=j+2;
          #print("les axes",x1,y1)
          abscice= x1;
          ordonne=y1;
          print(abscice,ordonne)
          #c=c+1;
         
         
          k=k+1; 
          x=train_x[k];
          j=0;
          i=0;       
      elif (i==(20)):
          i=0;
          j=j+1;
      else :
          j=0;
          i=i+1;
   
</code></pre>
<p>helpp meee</p>