ddbourgin / numpy-ml

Machine learning, in numpy
https://numpy-ml.readthedocs.io/
GNU General Public License v3.0
15.35k stars 3.72k forks source link

Bug in transfer learning #61

Closed RaulMurillo closed 3 years ago

RaulMurillo commented 3 years ago

System information

Describe the current behavior There is a problem when trying to perform simple transfer learning techniques (loading the same parameters from another trained layers/models). When setting layer params with a layer summary dictionary (generated with the summary() method), the activation function can be overridden with a string due to the non-exclusive if-clauses: https://github.com/ddbourgin/numpy-ml/blob/b537fac970f85d4919fe8c491499480a3a71bf8c/numpy_ml/neural_nets/layers/layers.py#L119-L127 This causes an error when trying to call the layer activation function in the forward() method.

Describe the expected behavior Layers that get their parameters with the set_params() method should behave without errors.

Code to reproduce the issue

>>> import numpy as np
>>> from numpy_ml.neural_nets.layers import *
>>>
>>> c1 = Conv2D(6, (3,3))
>>> c2 = Conv2D(6, (3,3))
>>> x = np.random.randn(1, 32, 32, 3)
>>>
>>> y1 = c1.forward(x)
>>> y2 = c2.forward(x) # No problem here
>>>
>>> c2.set_params(c1.summary()) # The act_fn of c2 is overridden as a str
<numpy_ml.neural_nets.layers.layers.Conv2D object at 0x7f0bf9d405f8>
>>> y3 = c2.forward(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ramuri01/numpy-ml/numpy_ml/neural_nets/layers/layers.py", line 2822, in forward
    Y = self.act_fn(Z)
TypeError: 'str' object is not callable