Skip to content

Commit

Permalink
np.eye handling, np.dot and np.zeros fix, dataset update
Browse files Browse the repository at this point in the history
Added np.eye for datatype forcing, Changed how the np.dot is being handled, fixed np.zeros issue, updated the datasets definitions
  • Loading branch information
daniel-kukiela committed Aug 21, 2020
1 parent dd4bf5e commit c2e216c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion nnfs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '0.5.0'
__version__ = '0.5.1'

from .core import init
15 changes: 8 additions & 7 deletions nnfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ def init(dot_precision_workaround=True, default_dtype='float32', random_seed=0):

# Numpy methods to be replaced for a workaround
methods_to_enclose = [
[np, 'zeros', False],
[np.random, 'randn', True],
[np, 'zeros', False, 1],
[np.random, 'randn', True, None],
[np, 'eye', False, 1],
]

# https://github.com/numpy/numpy/issues/15591
Expand All @@ -21,7 +22,7 @@ def dot(*args, **kwargs):
return orig_dot(*[a.astype('float64') for a in args], **kwargs).astype('float32')
np.dot = dot
else:
methods_to_enclose.append([np, 'dot', False])
methods_to_enclose.append([np, 'dot', True, None])

# https://github.com/numpy/numpy/issues/6860
# It is not possible to set default datatype with numpy
Expand All @@ -40,18 +41,18 @@ def dot(*args, **kwargs):
def enclose(method, default_dtype):

# Save a handler to original method
method.append(getattr(*method))
method.append(getattr(*method[:2]))
def enclosed_method(*args, **kwargs):

# If flag is True - use .astype()
if method[2]:
return method[3](*args, **kwargs).astype(default_dtype)
return method[4](*args, **kwargs).astype(default_dtype)

# Else pass dtype in kwargs
else:
if 'dtype' not in kwargs:
if len(args) <= method[3] and 'dtype' not in kwargs:
kwargs['dtype'] = default_dtype
return method[3](*args, **kwargs)
return method[4](*args, **kwargs)

# Replace numpy method with enclosed one
setattr(*method[:2], enclosed_method)
2 changes: 1 addition & 1 deletion nnfs/datasets/sine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


# Sine sample dataset
def create_data(samples=10000):
def create_data(samples=1000):

X = np.arange(samples).reshape(-1, 1) / samples
y = np.sin(2 * np.pi * X).reshape(-1, 1)
Expand Down
12 changes: 6 additions & 6 deletions nnfs/datasets/spiral.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
# Copyright (c) 2015 Andrej Karpathy
# License: https://github.com/cs231n/cs231n.github.io/blob/master/LICENSE
# Source: https://cs231n.github.io/neural-networks-case-study/
def create_data(points, classes):
X = np.zeros((points*classes, 2))
y = np.zeros(points*classes, dtype='uint8')
def create_data(samples, classes):
X = np.zeros((samples*classes, 2))
y = np.zeros(samples*classes, dtype='uint8')
for class_number in range(classes):
ix = range(points*class_number, points*(class_number+1))
r = np.linspace(0.0, 1, points)
t = np.linspace(class_number*4, (class_number+1)*4, points) + np.random.randn(points)*0.2
ix = range(samples*class_number, samples*(class_number+1))
r = np.linspace(0.0, 1, samples)
t = np.linspace(class_number*4, (class_number+1)*4, samples) + np.random.randn(samples)*0.2
X[ix] = np.c_[r*np.sin(t*2.5), r*np.cos(t*2.5)]
y[ix] = class_number
return X, y
10 changes: 5 additions & 5 deletions nnfs/datasets/vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
# Copyright (c) 2015 Andrej Karpathy
# License: https://github.com/cs231n/cs231n.github.io/blob/master/LICENSE
# Source: https://cs231n.github.io/neural-networks-case-study/
def create_data(points, classes):
X = np.zeros((points*classes, 2))
y = np.zeros(points*classes, dtype='uint8')
def create_data(samples, classes):
X = np.zeros((samples*classes, 2))
y = np.zeros(samples*classes, dtype='uint8')
for class_number in range(classes):
ix = range(points*class_number, points*(class_number+1))
X[ix] = np.c_[np.random.randn(points)*.1 + (class_number)/3, np.random.randn(points)*.1 + 0.5]
ix = range(samples*class_number, samples*(class_number+1))
X[ix] = np.c_[np.random.randn(samples)*.1 + (class_number)/3, np.random.randn(samples)*.1 + 0.5]
y[ix] = class_number
return X, y

0 comments on commit c2e216c

Please sign in to comment.