Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
antodima committed Apr 19, 2021
1 parent 7aeab0f commit b43fd02
Show file tree
Hide file tree
Showing 28 changed files with 17 additions and 33 deletions.
50 changes: 17 additions & 33 deletions LAB2_2/Assignment1/hopfield.m
Original file line number Diff line number Diff line change
@@ -1,59 +1,43 @@
% Hopfield model
% <https://elearning.di.unipi.it/pluginfile.php/46092/mod_resource/content/0/LAB2_2-Assignments.pd>
% <https://elearning.di.unipi.it/mod/resource/view.php?id=10695>

load('lab2_2_data');
set(groot,'defaultFigureVisible','off');

% distorted images of pattern 0
d_0_1_orig = distort_image(p0,0.05);
d_0_2_orig = distort_image(p0,0.1);
d_0_3_orig = distort_image(p0,0.25);
d_0_1 = d_0_1_orig;
d_0_2 = d_0_2_orig;
d_0_3 = d_0_3_orig;
d_0_1 = distort_image(p0,0.05); d_0_2 = distort_image(p0,0.1); d_0_3 = distort_image(p0,0.25);
% distorted images of pattern 1
d_1_1_orig = distort_image(p1,0.05);
d_1_2_orig = distort_image(p1,0.1);
d_1_3_orig = distort_image(p1,0.25);
d_1_1 = d_1_1_orig;
d_1_2 = d_1_2_orig;
d_1_3 = d_1_3_orig;
d_1_1 = distort_image(p1,0.05); d_1_2 = distort_image(p1,0.1); d_1_3 = distort_image(p1,0.25);
% distorted images of pattern 2
d_2_1_orig = distort_image(p2,0.05);
d_2_2_orig = distort_image(p2,0.1);
d_2_3_orig = distort_image(p2,0.25);
d_2_1 = d_2_1_orig;
d_2_2 = d_2_2_orig;
d_2_3 = d_2_3_orig;
%imagesc([reshape(d_0_1,32,32) reshape(d_0_2,32,32) reshape(d_0_3,32,32)]);
d_2_1 = distort_image(p2,0.05); d_2_2 = distort_image(p2,0.1); d_2_3 = distort_image(p2,0.25);
eps = 1;


% (1) storage phase (learning)
%% (1) storage phase (learning)
P = [p0';p1';p2'];
W = 1/1024 * (P' * P);
for i=1:1024
W(i,i) = 0;
end
I = ones(1024,1)*0.5; % bias

% (2) retrieval phase
% (2.1) initialization
O = [d_0_1_orig';d_0_2_orig';d_0_3_orig';d_1_1_orig';d_1_2_orig';d_1_3_orig';d_2_1_orig';d_2_2_orig';d_2_3_orig']; % original images
D = [d_0_1';d_0_2';d_0_3';d_1_1';d_1_2';d_1_3';d_2_1';d_2_2';d_2_3']; % distorted images
%% (2) retrieval phase
%% (2.1) initialization
O = [d_0_1';d_0_2';d_0_3';d_1_1';d_1_2';d_1_3';d_2_1';d_2_2';d_2_3']; % original images
D = O; % distorted images
L = [[0,0.05];[0,0.10];[0,0.25];[1,0.05];[1,0.10];[1,0.25];[2,0.05];[2,0.10];[2,0.25]]; % patterns info
Ps = [p0';p0';p0';p1';p1';p1';p2';p2';p2'];
Ps = [p0';p0';p0';p1';p1';p1';p2';p2';p2']; % patterns of each distorted images

% for each pattern distortions
for p=1:size(D,1)
epoch = 1;
overlaps = [];
energies = [];
pattern_num = L(p,1);
pattern_distortion = L(p,2);
energy_old = energy(W, I, D(p,:));
% (2.2) iteration until convergence
%% (2.2) iteration until convergence
while 1
% (2.3) choose i random neuron
%% (2.3) choose i random neuron
for i=randperm(1024)
D(p,i) = sign(W(i,:) * D(p,:)' + I(i));
overlaps = horzcat(overlaps,[overlap(p0, D(p,:)) overlap(p1, D(p,:)) overlap(p2, D(p,:))]');
Expand All @@ -69,22 +53,23 @@
epoch = epoch + 1;
end

%% plottings
disp('Saving figures . . .');
fig = figure;
hold on;
plot((1:size(overlaps,2)),overlaps(1,:));
plot((1:size(overlaps,2)),overlaps(2,:));
plot((1:size(overlaps,2)),overlaps(3,:));
title(sprintf('Overlaps (pattern=%d, distortion=%0.2f)',pattern_num,pattern_distortion));
xlabel('ticks')
xlabel('ticks');
ylabel('overlap function');
legend('overlap with pattern 0','overlap with pattern 1','overlap with pattern 2');
legend({'overlap with pattern 0','overlap with pattern 1','overlap with pattern 2'},'Location','southeast');
print(fig,sprintf('images/distorted_%d_%0.2f_overlap.png',pattern_num,pattern_distortion),'-dpng');
hold off;
fig = figure;
plot((1:size(energies,2)),energies(1,:));
title(sprintf('Energy (pattern=%d, distortion=%0.2f)',pattern_num,pattern_distortion));
xlabel('ticks')
xlabel('ticks');
ylabel('energy');
print(fig,sprintf('images/distorted_%d_%0.2f_energy.png',pattern_num,pattern_distortion),'-dpng');
fig = figure;
Expand All @@ -93,7 +78,6 @@
print(fig,sprintf('images/distorted_%d_%0.2f_reconstructed.png',pattern_num,pattern_distortion),'-dpng');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function m = overlap(p, x)
m = (p' * x') / 1024;
Expand Down
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.05_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.05_overlap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.05_reconstructed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.10_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.10_overlap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.10_reconstructed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.25_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.25_overlap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_0_0.25_reconstructed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.05_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.05_overlap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.05_reconstructed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.10_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.10_overlap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.10_reconstructed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.25_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.25_overlap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_1_0.25_reconstructed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.05_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.05_overlap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.05_reconstructed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.10_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.10_overlap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.10_reconstructed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.25_energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.25_overlap.png
Binary file modified LAB2_2/Assignment1/images/distorted_2_0.25_reconstructed.png

0 comments on commit b43fd02

Please sign in to comment.