uclnlp / jack

Jack the Reader
MIT License
257 stars 82 forks source link

Fix 289 #292

Closed pminervini closed 6 years ago

pminervini commented 6 years ago

Just replaced the following code in jack/util/map.py's numpify function:

            x_np = np.full(shape, pad, dtype)
            dims = len(shape)
            if dims == 0:
                x_np=x
            elif dims == 1:
                x_np[0:shape[0]] = x
            elif dims == 2:
                for j, y in enumerate(x):
                    x_np[j, 0:len(y)] = [ys for ys in y]#this comprehension turns DynamicSubsampledList into a list
            elif dims == 3:
                for j, ys in enumerate(x):
                    for k, y in enumerate(ys):
                        x_np[j, k, 0:len(y)] = y
            else:
                raise (NotImplementedError)
                # todo: extend to general case
                pass
            xs_np[key] = x_np

with the following recursive function - generalizes to dims > 3:

            x_np = np.full(shape, pad, dtype)
            nb_dims = len(shape)

            if nb_dims == 0:
                x_np = x
            else:
                def f(tensor, values):
                    t_shp = tensor.shape
                    if len(t_shp) > 1:
                        for _i, _values in enumerate(values):
                            f(tensor[_i], _values)
                    else:
                        tensor[0:len(values)] = [v for v in values]

                f(x_np, x)
            xs_np[key] = x_np

@tdmeeste @rockt in case it can be useful

dirkweissenborn commented 6 years ago

@pminervini this will most probably clash with #293 , which do you think we should merge first?

pminervini commented 6 years ago

@dirkweissenborn it's ok - let me get done with this pull asap