nengo / nengo-dl

Deep learning integration for Nengo
https://www.nengo.ai/nengo-dl
Other
88 stars 22 forks source link

Minibatch size cannot be larger than sample size #132

Closed arvoelke closed 4 years ago

arvoelke commented 4 years ago

Related to #121.

Minimal reproducer:

with nengo.Network() as model:
    nengo.Probe(nengo.Node(0))

with nengo_dl.Simulator(model, minibatch_size=2) as sim:
    sim.compile(loss=tf.losses.MSE)
    sim.evaluate(np.zeros((1, 1, 1)), np.zeros((1, 1, 1)), verbose=0)

Stack trace:

ValidationError                           Traceback (most recent call last)
<ipython-input-12-859a5caa84cc> in <module>
      4 with nengo_dl.Simulator(model, minibatch_size=2) as sim:
      5     sim.compile(loss=tf.losses.MSE)
----> 6     sim.evaluate(np.zeros((1, 1, 1)), np.zeros((1, 1, 1)), verbose=0)

~/anaconda3/envs/nengo-dl/lib/python3.7/site-packages/nengo/utils/magic.py in __call__(self, *args, **kwargs)
    179                 return self.wrapper(wrapped, instance, args, kwargs)
    180             else:
--> 181                 return self.wrapper(self.__wrapped__, self.instance, args, kwargs)
    182         else:
    183             instance = getattr(self.__wrapped__, "__self__", None)

~/git/nengo-dl/nengo_dl/simulator.py in require_open(wrapped, instance, args, kwargs)
     65         )
     66 
---> 67     return wrapped(*args, **kwargs)
     68 
     69 

~/git/nengo-dl/nengo_dl/simulator.py in evaluate(self, x, y, n_steps, stateful, **kwargs)
    888 
    889         return self._call_keras(
--> 890             "evaluate", x=x, y=y, n_steps=n_steps, stateful=stateful, **kwargs
    891         )
    892 

~/anaconda3/envs/nengo-dl/lib/python3.7/site-packages/nengo/utils/magic.py in __call__(self, *args, **kwargs)
    179                 return self.wrapper(wrapped, instance, args, kwargs)
    180             else:
--> 181                 return self.wrapper(self.__wrapped__, self.instance, args, kwargs)
    182         else:
    183             instance = getattr(self.__wrapped__, "__self__", None)

~/git/nengo-dl/nengo_dl/simulator.py in with_self(wrapped, instance, args, kwargs)
     49         instance.tensor_graph.device
     50     ):
---> 51         output = wrapped(*args, **kwargs)
     52     tf.keras.backend.set_floatx(keras_dtype)
     53 

~/git/nengo-dl/nengo_dl/simulator.py in _call_keras(self, func_type, x, y, n_steps, stateful, **kwargs)
    947             x,
    948             n_steps=n_steps,
--> 949             batch_size=self.minibatch_size if "on_batch" in func_type else None,
    950         )
    951 

~/git/nengo-dl/nengo_dl/simulator.py in _check_data(self, data, batch_size, n_steps, nodes)
   1854                     "Size of minibatch (%d) less than Simulation `minibatch_size` (%d)"
   1855                     % (x.shape[0], self.minibatch_size),
-> 1856                     "%s data" % name,
   1857                 )
   1858             if nodes and x.shape[1] % self.unroll != 0:

ValidationError: node data: Size of minibatch (0) less than Simulation `minibatch_size` (2)

Expected behaviour: I expected this to be okay, as it may not be that uncommon to want to evaluate some subset of the data (e.g., test data) that is smaller than the minibatch size. The error is also a little confusing because the sample size isn't 0 (it is 1).

drasmuss commented 4 years ago

In general it is expected that the minibatch size of the data has to match the minibatch size specified when creating the Simulator. It'd be good to allow flexible batch size (it's a planned future feature), but not anything that is implemented at the moment. We could fake it by zero-padding the data as a short-term fix, but then you're not really getting the benefits of running with a smaller batch size (in terms of performance).

That's definitely a bug in the error message though.