MLBazaar/MLPrimitives

Inferring data shapes with single dimension for `keras` adapter

sarahmish opened this issue · 0 comments

  • MLPrimitives version: 0.3.1
  • Python version: 3.7

Description

When having numpy arrays of single dimension, for example

X = np.array([0, 2, 1, 3, 5]) # shape (5, )

the keras adapter fails due to the _augment_hyperparameters function where we try to infer the shapes of the input and target variables. More specifically, the following lines can be edited

def _augment_hyperparameters(self, X, mode, kwargs):
    shape = np.asarray(X)[0].shape
    length = shape[0]
    self._setdefault(kwargs, '{}_shape'.format(mode), shape)
    self._setdefault(kwargs, '{}_dim'.format(mode), length)
    self._setdefault(kwargs, '{}_length'.format(mode), length)

    return kwargs

rather than assuming the data is at least contains two dimensions, i.e. shape = (m, n, ..) we can consume a shape of any size and cast it as uni-dimensional if we infer an empty tuple, more concretely the edited function would look like

def _augment_hyperparameters(self, X, mode, kwargs):
    shape = np.asarray(X)[0].shape
    if shape:
        length = shape[0]
    else:
        length = 1 # supporting shape (l, )

    self._setdefault(kwargs, '{}_shape'.format(mode), shape)
    self._setdefault(kwargs, '{}_dim'.format(mode), length)
    self._setdefault(kwargs, '{}_length'.format(mode), length)

    return kwargs

This issue was initially discovered in sintel-dev/Draco#59