-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
53 lines (42 loc) · 1.37 KB
/
functions.py
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
import numpy as np
#################
# TRAINNING
#################
def sigmoid(z):
return np.divide(1, 1+np.exp(-z))
def sigmoid_p(z):
return np.divide(np.exp(-z), np.power(1+np.exp(-z), 2))
# Only works for row vectors
def MSE(v1, v2):
return np.vdot(v1-v2, v1-v2) / (2*v1.shape[1])
#################
# DATA PROCESSING
#################
# Input is a 2 dimensional grayscale picture.
# Output is a row vector with the grayscale values.
def image_flat(image):
x_dim = image.shape[0]
flat = np.array([])
for i in range(x_dim):
flat = np.append(flat, image[i], axis = 0)
return np.array(flat, ndmin = 2) # Convert it into a row vector
# It is assumed all pictures have the same dimensions
def input_list(images):
inputs = []
for image in images:
if image.shape == ():
continue
inputs.append(image_flat(image))
return inputs
# The minimum value is assumed to be 0, the max value is max_value-1
# Output is a row vector
def one_hot_encoding(value, max_value):
vector = np.zeros((1, max_value))
vector[0, value] = 1
return vector
# Transforms a list of outputs into a matrix with outputs hot-encoded
def output_list(outputs, max_value):
output = []
for value in outputs:
output.append(one_hot_encoding(value, max_value))
return output