Skip to content
Snippets Groups Projects
Commit ed964016 authored by Vedrana Andersen Dahl's avatar Vedrana Andersen Dahl
Browse files

added Chapter08

parent 4787d240
No related branches found
No related tags found
No related merge requests found
% Generation of example data
example_nr = 3; % Three examples
n = 1000; % Number of points
noise = 1; % Noise level
% Make and show the data
[X,T,x,dim] = make_data(example_nr, n, noise);
cmap = interp1([-1; -0.5; 0; 0.5; 1],[0,0,0.5; 0,0.5,1; 1,1,1; 1,0,0; 0.5,0,0],linspace(-1,1,256));
X_colors = cmap([1+63,end-63],:);
figure
scatter(X(:,2),X(:,1),20,X_colors(T(:,1)+1,:),'filled','MarkerEdgeColor',[0 0 0])
box on, axis ij image, axis([0.5,dim(2)+0.5,0.5,dim(1)+0.5])
title('training')
%% Before training, you should make data have zero mean
c = mean(X,1);
xc = x-c;
Xc = X-c;
figure
scatter(Xc(:,2),Xc(:,1),20,X_colors(T(:,1)+1,:),'filled','MarkerEdgeColor',[0 0 0])
axis equal
box on, axis ij image, axis([0.5-c(2),dim(2)+0.5-c(2),0.5-c(1),dim(1)+0.5-c(1)])
title('Zero mean training')
function [X,T,x,dim] = make_data(example_nr, n, noise)
% Generate data for training a simple neural network.
%
% function [X,T,x,dim] = make_data(example_nr, n, noise)
%
% Input:
% example_nr - a number 1 - 3 for each example
% n - number of points in each data set
% noise - a number to increase or decrease the noise level (if changed,
% choose between 0.5 and 2)
% Output:
% X - 2n x 2 array of points (there are n points in each class)
% T - 2n x 2 target values
% x - regular sampled points on the area covered by the points that will
% be used for testing the neural network
% dim - dimensionality of the area covered by the points
%
% Authors: Vedrana Andersen Dahl and Anders Bjorholm Dahl - 25/3-2020
% vand@dtu.dk, abda@dtu.dk
%
if (nargin < 3)
noise = 1;
end
if (nargin < 2)
n = 200;
end
if ( mod(n,2) == 1 )
n = n+1;
end
dim = [100,100];
[QX,QY] = ndgrid(1:dim(1),1:dim(2));
x = [QX(:),QY(:)];
K = [n,n];
T = [ones(K(1),1)*[1 0];ones(K(2),1)*[0 1]];
switch example_nr
case 1
X = [noise*10*randn(K(1),2)+[30,30];noise*10*randn(K(2),2)+[70,70]];
case 2
rand_ang = rand(K(1),1)*2*pi;
X = [noise*5*randn(K(1),2) + 30*[cos(rand_ang), sin(rand_ang)];
noise*5*randn(K(2),2)] + (dim+1)/2;
case 3
X = [noise*10*randn(K(1)/2,2)+[30,30];noise*10*randn(K(1)/2,2)+[70,70];
10*randn(K(2)/2,2)+[30,70];10*randn(K(2)/2,2)+[70,30]];
otherwise
disp('Example number should be 1, 2, or 3.')
end
#%%
import numpy as np
def make_data(example_nr, n = 200, noise = 1):
'''
Generate data for training a simple neural network.
Arguments:
example_nr: a number 1 to 3 for each example.
n: number of points in each class set.
noise: noise level, best between 0.5 and 2.
Returns:
X: 2 x 2n array of points (there are n points in each class).
T: 2 x 2n target values.
x: grid points for testing the neural network.
dim: size of the area covered by the grid points.
Authors: Vedrana Andersen Dahl and Anders Bjorholm Dahl - 25/3-2020
vand@dtu.dk, abda@dtu.dk
'''
rg = np.random.default_rng()
dim = (100, 100)
QX, QY = np.meshgrid(range(0, dim[0]), range(0, dim[1]))
x_grid = np.c_[np.ravel(QX), np.ravel(QY)]
# Targets: first half class 0, second half class 1
T = np.vstack((np.tile([True, False], (n, 1)),
np.tile([False, True], (n, 1))))
if example_nr == 1 : # two separated clusters
X = np.vstack((np.tile([30., 30.], (n, 1)),
np.tile([70., 70.], (n, 1))))
X += rg.normal(size=X.shape, scale=10*noise) # add noise
elif example_nr == 2 : # concentric clusters
rand_ang = 2 * np.pi * rg.uniform(size=n)
X = np.vstack((30 * np.array([np.cos(rand_ang), np.sin(rand_ang)]).T,
np.tile([0., 0.], (n, 1))))
X += [50, 50] # center
X += rg.normal(size=X.shape, scale=5*noise)# add noise
elif example_nr == 3 : # 2x2 checkerboard
n1 = n//2
n2 = n//2 + n%2 # if n is odd n2 will have 1 element more
X = np.vstack((np.tile([30., 30.], (n1, 1)),
np.tile([70., 70.], (n2, 1)),
np.tile([30. ,70.], (n1, 1)),
np.tile([70., 30.], (n2, 1))))
X += rg.normal(size=X.shape, scale=10*noise) # add noise
else:
print('No data returned - example_nr must be 1, 2, or 3')
o = rg.permutation(range(2*n))
return X[o].T, T[o].T, x_grid.T, dim
#%% Test of the data generation
if __name__ == "__main__":
#%%
import matplotlib.pyplot as plt
n = 1000
noise = 1
fig, ax = plt.subplots(1, 3)
for i, a in enumerate(ax):
example_nr = i + 1
X, T, x_grid, dim = make_data(example_nr, n, noise)
a.scatter(X[0][T[0]], X[1][T[0]], c='r', alpha=0.3, s=15)
a.scatter(X[0][T[1]], X[1][T[1]], c='g', alpha=0.3, s=15)
a.set_aspect('equal', 'box')
a.set_title(f'Example {i} data')
plt.show()
#%% Before training, you should make data zero mean
c = np.mean(X, axis=1, keepdims=True)
X_c = X - c
fig, ax = plt.subplots(1,1)
ax.scatter(X_c[0][T[0]], X_c[1][T[0]], c='r', alpha=0.3, s=15)
ax.scatter(X_c[0][T[1]], X_c[1][T[1]], c='g', alpha=0.3, s=15)
ax.set_aspect('equal', 'box')
plt.title('Zero-mean data')
plt.show()
# %%
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment