perceptron_tut.py 1.5 KB
Newer Older
Hannes Hinrichs's avatar
Hannes Hinrichs committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

"""
Tutorial Machine Learning II - 19.04.2018
(Single Layer Perceptron)
"""

import numpy as np
import matplotlib.pyplot as plt

def generate_blobs(n_Data, cluster_label, n_dim, distance, shift):
	n_Cluster = cluster_label.size
	n_Classes = len(set(cluster_label))
	x=np.zeros([n_Data,n_dim],dtype=float)
	label=np.zeros([n_Data,n_Classes],dtype=int)
	cov=np.zeros([n_dim,n_dim],dtype=float)
	np.fill_diagonal(cov,1)
	for i in range(n_Cluster):
		start=int(np.ceil(n_Data/float(n_Cluster)*i))
		finish=int(np.floor(n_Data/float(n_Cluster)*(i+1)))
		x[start:finish,:]=np.random.multivariate_normal([(i//2)*distance,(i%2)*distance-shift],cov,size=n_Data//n_Cluster)
		label[start:finish,cluster_label[i]]=1

	return x, label
		
def initialize_weights(n_input,n_output):
	return np.random.normal(0,0.01,[n_input, n_output])

def feedforward(x,w1):
	################
	# your code here
	################
	
	return activation

def train(x,w1,label,eps):
	################
	# your code here
	################
	return w1


##############################################################################
# MAIN
	

# Settings:
cluster_label=np.array([0,1])
n_Data_per_Cluster = 1000
dim = 2
distance = 20
shift = 10
learning_rate = 0.01

####

n_Cluster=cluster_label.size
n_Data = n_Data_per_Cluster * n_Cluster

x,label = generate_blobs(n_Data, cluster_label, dim, distance, shift)
s = np.arange(x.shape[0])
np.random.shuffle(s)
x = x[s,:]
label = label[s,:]


# Plot:
plt.figure()
plt.scatter(x[:,0],x[:,1], c=np.argmax(label,axis=1))
plt.show