Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hopfield Networks MacKay - Chapter 42.

Similar presentations


Presentation on theme: "Hopfield Networks MacKay - Chapter 42."β€” Presentation transcript:

1 Hopfield Networks MacKay - Chapter 42

2

3 The Story So Far Feedforward networks:
All connections are directed – the activity from each neuron only influences downstream neurons.

4 Feedback Networks Feedback Networks:
Any network that is not a feedforward network. Connections may be bi-directional.

5 Hopfield Networks Consists of I fully connected neurons.
All connections are bidirectional such that wij = wji. There are no self-connections (i.e. wii = 0). Activation: π‘Ž 𝑖 = 𝑗 𝑀 𝑖𝑗 π‘₯ 𝑗 State Updates: π‘₯ 𝑖 =π‘‘π‘Žπ‘›β„Ž π‘Ž 𝑖 2 w12 1 w21 4 w34 3 w43

6 Hopfield Energy Function
Optional bias term 𝐸=βˆ’ 𝑖<𝑗 π‘₯ 𝑖 π‘₯ 𝑗 𝑀 𝑖𝑗 βˆ’ 𝑖 π‘₯ 𝑖 𝑀 0 This objective of the Hopfield network is to minimize the energy function. The effect of a single π‘₯ 𝑖 on the global energy can be computed as: Ξ” 𝐸 𝑖 =𝐸 π‘₯ 𝑖 =0 βˆ’πΈ π‘₯ 𝑖 =1 = 𝑀 0 + 𝑗 π‘₯ 𝑗 𝑀 𝑖𝑗 Energy when π‘₯ 𝑖 is off Energy when π‘₯ 𝑖 is on

7 Settling to an Energy Minimum
-4 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 -1 -1

8 Settling to an Energy Minimum
Start from a random state -4 1 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 1 -1 -1 -E = goodness = 3 (Look at all pairs of units that are on and add up the weights between them).

9 Settling to an Energy Minimum
What state should this unit be in given the states of all the other units? -4 1 ? To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 1 -1 -1 -E = goodness = 3 (Look at all pairs of units that are on and add up the weights between them).

10 Settling to an Energy Minimum
Input = 1x-4 + 0x3 + 0x3 = -4 Since -4 < 0, we turn the unit off. -4 1 ? To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 1 -1 -1 -E = goodness = 3 (Look at all pairs of units that are on and add up the weights between them).

11 Settling to an Energy Minimum
Input = 1x-4 + 0x3 + 0x3 = -4 Since -4 < 0, we turn the unit off. -4 1 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 1 -1 -1 -E = goodness = 3 (Look at all pairs of units that are on and add up the weights between them).

12 Settling to an Energy Minimum
-4 1 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 ? -1 -1 -E = goodness = 3 (Look at all pairs of units that are on and add up the weights between them).

13 Settling to an Energy Minimum
-4 1 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 ? -1 -1 Input = 1x3 + 0x-1 = 3 Since 3 > 0, we turn the unit on. -E = goodness = 3 (Look at all pairs of units that are on and add up the weights between them).

14 Settling to an Energy Minimum
-4 1 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 1 -1 -1 Input = 1x3 + 0x-1 = 3 Since 3 > 0, we turn the unit on. -E = goodness = 3 (Look at all pairs of units that are on and add up the weights between them).

15 Settling to an Energy Minimum
-4 1 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 1 -1 ? -1 Input = 1x2 + 1x-1 + 0x3 + 0x-1 = 1 Since 1 > 0, we turn the unit on. -E = goodness = 3 (Look at all pairs of units that are on and add up the weights between them).

16 Settling to an Energy Minimum
-4 1 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 1 -1 1 -1 Input = 1x2 + 1x-1 + 0x3 + 0x-1 = 1 Since 1 > 0, we turn the unit on. -E = goodness = 4 (Look at all pairs of units that are on and add up the weights between them).

17 Settling to an Energy Minimum
-4 1 To find an energy minimum in this net, start from a random state and then update the units one at a time in random order. Update each unit to whichever of its two states (0, or 1) gives the lowest global energy. 3 2 3 3 1 -1 1 -1 Now the network has settled to a minimum (none of the units want to change states). -E = goodness = 4 (Look at all pairs of units that are on and add up the weights between them).

18 A Deeper Energy Minimum
-4 1 This network has a second stable energy minimum. 3 2 3 3 -1 1 -1 1 -E = goodness = 5

19 A Deeper Energy Minimum
-4 1 The net has two triangles in which the three units mostly support each other. Each triangle mostly dislikes the other triangle. The triangle on the left differs from the one on the right by having a weight of 2 where the other one has a weight of 3. So turning on the units in the triangle on the right gives the deepest minimum. 3 2 3 3 -1 1 -1 1 -E = goodness = 5

20 Hopfield Networks Weights are initialized using the Hebb rule: E.g.
For k patterns: 𝑀 𝑖𝑗 = π‘˜ π‘₯ 𝑖 (π‘˜) π‘₯ 𝑗 (π‘˜) Where π‘₯ 𝑖 , π‘₯ 𝑗 ∈ βˆ’1,1 E.g. π‘₯ 𝑖 1βˆ’4 =[1 1 βˆ’1 1] π‘₯ 𝑗 1βˆ’4 =[1 1 βˆ’1 1] 𝑀 𝑖𝑗 = 1βˆ™1 + 1βˆ™1 + βˆ’1βˆ™βˆ’1 + 1βˆ™1 =4 π‘₯ 𝑗 1βˆ’4 =[βˆ’1 βˆ’1 1 βˆ’1] 𝑀 𝑖𝑗 = 1βˆ™βˆ’1 + 1βˆ™βˆ’1 + βˆ’1βˆ™1 + 1βˆ™βˆ’1 =βˆ’4

21 Hopfield Networks For a single corrupted pattern 𝑋 (π‘˜) , convergence to one of the memorized patterns proceeds as follows: π‘Ž 𝑖 = 𝑗 𝑀 𝑖𝑗 π‘₯ 𝑗 π‘₯ 𝑖 =π‘‘π‘Žπ‘›β„Ž π‘Ž 𝑖 πœ– 𝑖 = π‘₯ 𝑖 𝑛𝑒𝑀 βˆ’ π‘₯ 𝑖 π‘œπ‘™π‘‘ πœ•π‘€= π‘₯ 𝑛𝑒𝑀 βˆ— πœ– 𝑇 πœ•π‘€β†πœ•π‘€+ πœ•π‘€ 𝑇 π‘Šβ†π‘Š+πœ‚ πœ•π‘€βˆ’π›Όπ‘Š Until the summed error ( 𝑖 πœ– 𝑖 ) reaches some desired minimum.

22 Matlab Code M = 5; % Number of rows in memories. N = 5; % Number of columns in memories. MN = M*N; % Number of nodes in the network eta = 1/MN; % Learning rate alpha = 1; % Regularizer constant max_iter = 100; % Maximum number of iterations. epsilon = 1e-4; % Tolerance on convergence.

23 Matlab Code % Memories % These are the images we want to network to remember and % to be attracted to on future presentations of noisy stimuli Mems = ones(M,N,4); % Character 'D': Mems(1,1:4,1) = -1; Mems(2:4,[2,5],1) = -1; Mems(5,2:4,1) = -1; % Character 'J': Mems(1,:,2) = -1; Mems(2:4,4,2) = -1; Mems(4:5,1,2) = -1; Mems(5,2:3,2) = -1; % Character 'C': Mems([1,5],2:5,3) = -1; Mems(2:4,1,3) = -1; % Character 'M': Mems(:,[1,5],4) = -1; Mems(2,[2,4],4) = -1; Mems(3,3,4) = -1; nMem = size(Mems,3);

24 Matlab Code % Plot the memories figure('Color','w'); for n = 1:nMem subplot(1,nMem,n); imagesc(Mems(:,:,n)); colormap('gray'); axis equal; axis tight; axis off; end

25 Matlab Code tmpWi = zeros(MN,MN,nMem); tmpWj = zeros(MN,MN,nMem); for k = 1:nMem [tmpWj(:,:,k),tmpWi(:,:,k)] = meshgrid(reshape(Mems(:,:,k),MN,1)); end % % Initialize the weights using Hebb rule W = sum(tmpWi .* tmpWj,3); % Set diagonal to zero: W = W .* ~eye(MN,MN); % Ensure the self-weights are zero

26 Matlab Code % The letter number to corrupt num_to_corr = 1; % Number of bits to flip num_to_flip = 5; % Randomly flip num_to_flip pixels in the num_to_corr image memory to create % Xinit, the initial state matrix: Xinit = Mems(:,:,num_to_corr); if num_to_flip>0 randinds = randperm(MN); flipinds = randinds(1:num_to_flip); Xinit(flipinds) = -1 * Xinit(flipinds); end

27 Matlab Code % Plot the corrupted letter that we're going to pass to the network figure; imagesc(Xinit); axis off; axis image; colormap gray; title('X at iteration 0');

28 Matlab Code % Initialize variables converged = 0; iter = 1; Xhat = Xinit; Xold = ones(M,N);

29 Matlab Code while ~converged && iter<=max_iter % a(j) = sum(w(j,l)*x(l)) + theta(j) Activations = W * Xhat(:); % compute all activations % h(j) = tanh(a(j)) Xhat = reshape(tanh(Activations),M,N); % compute all outputs π‘Ž 𝑖 = 𝑗 𝑀 𝑖𝑗 π‘₯ 𝑗 π‘₯ 𝑖 =π‘‘π‘Žπ‘›β„Ž π‘Ž 𝑖

30 Matlab Code % Show the current state of the network figure; imagesc(Xhat); axis off; axis image; colormap gray; title(['X at iteration ',num2str(iter)]);

31 Matlab Code % Measure how much the weights have changed since the last iteration err = sum(abs(Xhat(:)-Xold(:))); if err<=epsilon converged = 1; fprintf('Network has converged at iteration %i.\n',iter); else fprintf('Difference between current and previous state: %f.\n',err); end

32 Matlab Code πœ– 𝑖 = π‘₯ 𝑖 𝑛𝑒𝑀 βˆ’ π‘₯ 𝑖 π‘œπ‘™π‘‘ πœ•π‘€= π‘₯ 𝑛𝑒𝑀 βˆ— πœ– 𝑇 πœ•π‘€β†πœ•π‘€+ πœ•π‘€ 𝑇
πœ– 𝑖 = π‘₯ 𝑖 𝑛𝑒𝑀 βˆ’ π‘₯ 𝑖 π‘œπ‘™π‘‘ e = Xhat(:)-Xold(:); % compute all errors gw = Xhat(:)*e(:)'; % compute the gradient on the errors with respect to the weights gw = gw + gw'; % symmetrize gradient W = W + eta * ( gw - alpha * W ); % make step iter = iter + 1; Xold = Xhat; end πœ•π‘€= π‘₯ 𝑛𝑒𝑀 βˆ— πœ– 𝑇 πœ•π‘€β†πœ•π‘€+ πœ•π‘€ 𝑇 π‘Šβ†π‘Š+πœ‚ πœ•π‘€βˆ’π›Όπ‘Š

33 Hopfield Networks Can easily be adapted to match a degraded pattern to a given pattern from a set.

34 Hopfield Networks dBD = distance between B and D.
Hopfield & Tank (1985) Hopfield Networks dBD = distance between B and D.

35 Hopfield Networks Hopfield and Tank (1985) showed how Hopfield networks can be used to solve the traveling salesman problem.

36 Homework Try adapting the Hopfield network code to handle a 5th letter (e.g. β€œL”).


Download ppt "Hopfield Networks MacKay - Chapter 42."

Similar presentations


Ads by Google