Error in extraction tensor

I want to extract the cordonne, the code is

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; 

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

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:

a = torch.tensor([True])
if a: # works
    print('works')
    
b = torch.tensor([True, False])
if b: # fails
    print('fails')
# > RuntimeError: Boolean value of Tensor with more than one value is ambiguous

so you would have to check all conditions and make sure they return a valid single bool value.

On the other hand, your condition equals to …

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 
        ...

hi @Eta_C @ptrblck thanks for this your reply, but I have other problem

    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;
   

TypeError: unhashable type: ‘slice’
help me plzz

@ptrblck @Eta_C I modified with this code, but this code executes but returns no results

 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;
   

helpp meee