-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
50 lines (38 loc) · 1.43 KB
/
Copy pathutilities.py
File metadata and controls
50 lines (38 loc) · 1.43 KB
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
#!/bin/python
import numpy as np
import itertools
import json
np.random.seed(1)
"""" This file contains utility functions for neural networks """
def one_hot_encode(vector, skip_null=False):
""" One Hot Encoding of a whole number valued vector.
If skip_null is true, a vector of all zeros is ignored
"""
vector = np.array(vector)
vector = vector.flatten()
num_classes = np.unique(vector)
minimum = min(vector)
maximum = max(vector)
if skip_null:
return np.array([[1 if i == j else 0 for i in range(minimum, maximum + 1)] for j in vector])
else:
return np.array([[1 if i == j else 0 for i in range(minimum, maximum + 1)] for j in vector])
def zero_vector_p(vector):
""" Returns True if the vector is the zero vector """
return False if np.count_nonzero(vector) else True
def one_hot_decode(matrix):
""" Takes a one hot encoded numpy array and returns a vector of the classes """
if np.ndim(matrix) != 1:
return np.array([np.argmax(vector) for vector in matrix])
else:
return np.array(np.argmax(matrix))
def round_to_int(x):
""" Rounds the array, and returns it with entries as numpy integers """
x = np.array(x)
return np.round(x).astype(int)
def load_network(file_name, **kwargs):
""" Load a network from file *file_name* """
n = NeuralNetwork()
weights = np.load(file_name, **kwargs)
n.weights = weights
return n