<jupyter_start><jupyter_text>Usando chamfer loss<jupyter_code>dataset = MNISTSet(train=False, full=True)
expected, sample,mask_sample = dataset[13]
sample = sample.unsqueeze(0)
mask_sample = mask_sample.unsqueeze(0)
expected = torch.tensor(expected).unsqueeze(0)
expected.shape
sample.shape
sample.dtype
mask_sample.shape
sample.dtype
fig = plt.figure(figsize=(10, 10))
for i in range(500):
    input, target_set, target_mask = expected,sample,mask_sample
    if(i == 1):
        print(input)
    (progress, masks, evals, gradn), (y_enc, y_label) = net(input, target_set, target_mask)
    if i == 0:
        # =============
        # First subplot
        # =============
        # set up the axes for the first plot
        ax = fig.add_subplot(2, 2, 1)
        s = progress[10].squeeze(0).detach().numpy()
        x = s[1, :]
        y = s[0, :]
        mnsitSet = ax.scatter(x, y,color ='b')
        plt.axis([0, 1, 1, 0])
        plt.title('Epoch 1')
    if i == 14:
        #plt.title('Label: {}'.format(y_label[0].item()))
        # =============
        # Second subplot
        # =============
        # set up the axes for the first plot
        ax = fig.add_subplot(2, 2, 2)
        # plot a 3D surface like in the example mplot3d/surface3d_demo
        s = progress[4].squeeze(0).detach().numpy()
        x = s[1, :]
        y = s[0, :]
        mnsitSet = ax.scatter(x, y,color ='r')
        plt.axis([0, 1, 1, 0])
        plt.title('Epoch 25')
    if i == 49:
        # =============
        # Third subplot
        # =============
        # set up the axes for the first plot
        ax = fig.add_subplot(2, 2, 3)
        # plot a 3D surface like in the example mplot3d/surface3d_demo
        s = progress[10].squeeze(0).detach().numpy()
        x = s[1, :]
        y = s[0, :]
        mnsitSet = ax.scatter(x, y,color ='y')
        plt.axis([0, 1, 1, 0])
        plt.title('Epoch 50')
    if i == 99:
        # =============
        # Forth subplot
        # =============
        # set up the axes for the first plot
        ax = fig.add_subplot(2, 2, 4)
        # plot a 3D surface like in the example mplot3d/surface3d_demo
        s = progress[10].squeeze(0).detach().numpy()
        x = s[1, :]
        y = s[0, :]
        mnsitSet = ax.scatter(x, y,color ='g')
        plt.axis([0, 1, 1, 0])
        plt.title('Epoch 100')
    progress_only = progress

  # if using mask as feature, concat mask feature into progress
    target_set = torch.cat(
      [target_set, target_mask.unsqueeze(dim=1)], dim=1)
    progress = [
      torch.cat([p, m.unsqueeze(dim=1)], dim=1)
      for p, m in zip(progress, masks)
            ]
  # dim 0 is over the inner iteration steps
  # target set is broadcasted over dim 0
    set_loss = chamfer_loss(
      torch.stack(progress), target_set.unsqueeze(0)
    )
    loss = set_loss.mean()
    #print(loss.item())
    #print(loss)
    progress = progress_only

    #print("New progress",len(progress))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
plt.show()
#with torch.no_grad():
(progress, masks, evals, gradn), (y_enc, y_label) = net(input,sample,mask_sample)
progress[0].shape
sam = progress[10].squeeze(0).detach().numpy()
dataset = MNISTSet(train=False, full=True)
expected, sample,mask_sample = dataset[13]
x = sample[1, :]
y = sample[0, :]
pyplot.scatter(x, y,color ='b')

x = sam[1, :]
y = sam[0, :]
pyplot.scatter(x, y,marker = 'x',color ='r')

pyplot.axis([0, 1, 1, 0])
pyplot.title('Label: {}'.format(expected))
#plt.savefig('test.png')
pyplot.show()

x = sample[1, :]
y = sample[0, :]
pyplot.scatter(x, y,color ='b')
pyplot.axis([0.1, 1, 1, 0.1])
pyplot.title('Label: {}'.format(expected))

pyplot.show()
x = sam[1, :]
y = sam[0, :]
pyplot.scatter(x, y,marker = 'x',color ='r')
pyplot.axis([0.1, 1, 1, 0.1])
pyplot.title('Label: {}'.format(expected))
#plt.savefig('test.png')
pyplot.show()

sample = sample.unsqueeze(0)
mask_sample = mask_sample.unsqueeze(0)
expected = torch.tensor(expected).unsqueeze(0)
type(sample)<jupyter_output><empty_output><jupyter_text>Usando Hungarian<jupyter_code>netHungarian = build_net()
optimizer = torch.optim.Adam(
    [p for p in netHungarian.parameters() if p.requires_grad], lr=0.03
)
for i in range(100):
    #input, target_set, target_mask = map(lambda x: x, sample)
    if(i == 1):
        print(input)
    input, target_set, target_mask = expected,sample,mask_sample
    (progress, masks, evals, gradn), (y_enc, y_label) = netHungarian(input, target_set, target_mask)
    if i == 0 or  i == 25 or i ==  50 or i == 99:
    #plt.grid(False)
    #plt.tight_layout()
    #print(len(progress))
        for  a in progress:
          #print(a.shape)
            #a = a.cpu().data.numpy()
            sam = a[0].detach().numpy()
            x = sam[1, :]
            y = sam[0, :]
            pyplot.scatter(x, y,color ='b')
            plt.axis([0, 1, 1, 0])
            plt.title('Label: {}'.format(input[0]))
            #plt.savefig('test.png')
            plt.show()
            progress_only = progress

  # if using mask as feature, concat mask feature into progress
    target_set = torch.cat(
      [target_set, target_mask.unsqueeze(dim=1)], dim=1)
    progress = [
      torch.cat([p, m.unsqueeze(dim=1)], dim=1)
      for p, m in zip(progress, masks)
            ]
  # dim 0 is over the inner iteration steps
  # target set is broadcasted over dim 0
    a = torch.stack(progress)
  # target set is explicitly broadcasted over dim 0
    b = target_set.repeat(a.size(0), 1, 1, 1)
  # flatten inner iteration dim and batch dim
    a = a.view(-1, a.size(2), a.size(3))
    b = b.view(-1, b.size(2), b.size(3))
    set_loss = hungarian_loss(
      progress[-1], target_set
    ).unsqueeze(0)
    loss = set_loss.mean()
    #print(set_loss)
    print(loss)
    progress = progress_only

    #print("New progress",len(progress))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

#with torch.no_grad():
(progress, masks, evals, gradn), (y_enc, y_label) = netHungarian(input,sample,mask_sample)
progress[0].shape
sam = progress[10].squeeze(0).detach().numpy()
dataset = MNISTSet(train=False, full=True)
expected, sample,mask_sample = dataset[13]
x = sample[1, :]
y = sample[0, :]
pyplot.scatter(x, y,color ='b')

x = sam[1, :]
y = sam[0, :]
pyplot.scatter(x, y,marker = 'x',color ='r')

pyplot.axis([0.15, 0.95, 0.95, 0.15])
pyplot.title('Label: {}'.format(expected))
#plt.savefig('test.png')
pyplot.show()

x = sample[1, :]
y = sample[0, :]
pyplot.scatter(x, y,color ='b')
pyplot.axis([0.1, 1, 1, 0.1])
pyplot.title('Label: {}'.format(expected))

pyplot.show()
x = sam[1, :]
y = sam[0, :]
pyplot.scatter(x, y,marker = 'x',color ='r')
pyplot.axis([0.1, 1, 1, 0.1])
pyplot.title('Label: {}'.format(expected))
#plt.savefig('test.png')
pyplot.show()

sample = sample.unsqueeze(0)
mask_sample = mask_sample.unsqueeze(0)
expected = torch.tensor(expected).unsqueeze(0)
type(sample)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1788594470046083, 'fraction_numerical': 0.029809907834101382, 'mean_word_length': 2.9959723820483313, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '379369', 'license': 'MIT', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2607, 'n_words': 583, 'path': 'Tutorials/DeepSets  MnistSet/DSPN-Autoencoder.ipynb', 'repo_name': 'AhmedBegggaUA/SetXAI', 'size': '6944'}