jorgenkg / python-neural-network

This is an efficient implementation of a fully connected neural network in NumPy. The network can be trained by a variety of learning algorithms: backpropagation, resilient backpropagation and scaled conjugate gradient learning. The network has been developed with PYPY in mind.
BSD 2-Clause "Simplified" License
297 stars 98 forks source link

Error shapes not alligned when creating Instance with numpy array #19

Open KamodaP opened 7 years ago

KamodaP commented 7 years ago

It is quite common in data science apps to use numpy arrays and pandas dataframes. Unfortunately when you try to create Instance using numpy array of shape (1,x) your code creates in one of the places array of shape (nobs, x, 1) which does not work in dot when multiplied by array of shape (x, nlayers). It could be easily checked with the verify_dataset_shape_and_modify. Here's some snippet:

def mult(x):
     res = x[0]
     for a in x[1:]:
         res *= a
     return res
[...]
def verify_dataset_shape_and_modify(...)
    [...]
    data              = np.array( [instance.features.reshape((mult(instance.features.shape),) \
                                         if type(instance.features) == np.ndarray\ #more generalization perhaps?
                                         else instance.features\
                                         for instance in dataset ] )

whith this snippet even accidentally passing arrays of shape (x,1) will work.

jorgenkg commented 7 years ago

You are correct. The documentation states that an instance should be initialized with a built-in one-dimensional python list, e.g. Instance( [0, 1] ). However, as you pint out there are currently no checks that verify whether the argument is indeed a python list.

I'll add validation check to my todo list, thank you for pointing it out