Commit 30ddfdcc authored by Artem Oppermann's avatar Artem Oppermann

working version

parent 124b5c26
import numpy as np
import random
csv_path='./data.txt'
class Rectangle:
def __init__(self, x, y, width, height):
self.x=x
self.y=y
self.width=width
self.height=height
self.matrix_size=10
self.matrix=self._init_zeros_matrix()
self.fill()
def _init_zeros_matrix(self):
return np.zeros(shape=(self.matrix_size, self.matrix_size), dtype=np.float32)
def add_noise(self):
new_matrix=np.zeros(shape=(self.matrix_size, self.matrix_size), dtype=np.float32)
for i in range(10):
for j in range(10):
new_matrix[i][j]+=self.matrix[i][j]+np.random.poisson(lam=2.0)
return new_matrix
def fill(self):
for i in range(self.width):
for j in range(self.height):
self.matrix[self.x+i][self.y+j]=10.0
def get_matrix(self):
return self.matrix
def gen_data():
rectangles={0:Rectangle(0,0,2,2),
1:Rectangle(3,3,2,2),
2:Rectangle(5,5,2,2),
3:Rectangle(7,7,2,2),}
rect_data=[]
labels=[]
data=[]
for i in range(0,1000):
label=i%4
rect=np.array(rectangles[label].add_noise())
rect_reshaped=np.reshape(rect,[1,100])
data_sample=[]
data_sample.append(rect_reshaped)
data_sample.append(label)
data.append(data_sample)
return data
import numpy as np
from data_gen import gen_data
import random
n_input = 100
n_output=4
val_after_iter=25
n_samples=1000
learning_rate=0.05
class Model:
def __init__(self, nodes=[n_input, 25, n_output]):
self.nodes=nodes
self.num_layer=len(self.nodes)
self.weights=[]
for i in range(0,self.num_layer-1):
temp_weights=np.random.normal(loc=0.0, scale=0.4, size=(self.nodes[i], self.nodes[i+1]))
self.weights.append(temp_weights)
def tanh(self, x, derivative):
if not derivative:
return np.tanh(x)
else:
return (1-(np.tanh(x)**2))
def sigmoid(self,x , derivative):
if not derivative:
return 1/(1+np.exp(-x))
else:
return self.sigmoid(x, False)*(1-self.sigmoid(x, False))
def relu(self, x, derivative):
if not derivative:
return x*(x>0)
else:
return 1*(x>0)
def activation(self, x, derivative=False, f='sigmoid'):
'''Activation function (sigmoid by default)
@param x: input data
@param derivative: boolean if we need a derivative of the sigmoid'''
if f=='sigmoid':
a=self.sigmoid
elif f=='tanh':
a=self.tanh
elif f=='relu':
a=self.relu
if not derivative:
return a(x, False)
else:
return a(x,True)
def forward_step(self, x):
z_array=[] # dot product solution, before activation
a_array=[]
a_array.append(x) # the inner states
outputs=[]
#Forward propagation
for i in range(0, self.num_layer-1):
z=np.dot(self.weights[i].T,a_array[i])
z_array.append(z)
a=self.activation(z_array[i], False)
a_array.append(a)
outputs.append(a_array[-1])
return outputs
def mean_squared_error(self, output, target):
return np.sum(np.power(target-output,2))/len(output)
def accuracy(self, output, target):
mean_error=np.sum(abs(output-target))/len(output)
return (1-mean_error)
def train(self, data):
random.shuffle(data)
for epoch in range(0,1):
outputs=[]
error=0
for n in range(0,n_samples):
x=data[n][0]
label=data[n][1]
x=np.reshape(x, [100,1])
zeros=np.zeros(shape=[4,1])
zeros[label]=1
y=zeros
z_=[]
a_=[]
a_.append(x)
delta=[]
dEdW=[]
for i in range(0, self.num_layer-1):
z=np.dot(self.weights[i].T,a_[i])
z_.append(z)
a=self.activation(z_[i], False)
a_.append(a)
outputs.append(a_[-1])
temp_delta=-(y-a_[-1])*self.activation(z_[-1],True)
delta.append(temp_delta)
temp_dEdW=np.outer(a_[-2],temp_delta)
dEdW.append(temp_dEdW)
for i in range(0, self.num_layer-2):
temp_delta=np.dot(self.weights[self.num_layer-(i+2)],delta[i])*self.activation(z_[self.num_layer-(i+3)],True)
delta.append(temp_delta)
temp_dEdW=np.outer(a_[len(a_)-(i+3)],temp_delta)
dEdW.append(temp_dEdW)
#Updating weights
for i in range(0, self.num_layer-1):
self.weights[i]=self.weights[i]-learning_rate*dEdW[self.num_layer-(i+2)]
e_=self.mean_squared_error(a_[-1],y)
error+=e_
if n>0 and n%val_after_iter==0:
print('epoch_nr.: %i, n_sample: %i, mse: %.3f' %(epoch, n, (error/val_after_iter)))
error=0
data=gen_data()
model=Model()
model.train(data)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment