Example #1
0
def set_up_layer(layer, specs):
    layer.set_handler(HANDLER)
    time_steps = specs.get('time_steps', 3)
    batch_size = specs.get('batch_size', 2)
    hubs, layout = create_layout({'test_layer': layer})
    total_size, slices, shapes = get_total_size_slices_and_shapes(
        hubs, time_steps, batch_size)
    full_buffer = HANDLER.allocate((total_size, ))
    buffers = [
        full_buffer[slice_].reshape(shape)
        for slice_, shape in zip(slices, shapes)
    ]
    view = create_buffer_views_from_layout(layout, buffers, hubs)

    layer_buffers = view.test_layer

    # init parameters randomly
    HANDLER.set_from_numpy(view.parameters,
                           np.random.randn(view.parameters.size) * 1)

    inits = specs.get('inits', {})
    for param_name, value in inits.items():
        HANDLER.set_from_numpy(view.test_layer.parameters[param_name], value)

    for key, value in view.test_layer.inputs.items():
        if key in specs:  # if a special input is given use that
            # print("Using special input:", key)
            HANDLER.set_from_numpy(layer_buffers.inputs[key], specs[key])
        else:  # otherwise randomize the input
            HANDLER.set_from_numpy(
                layer_buffers.inputs[key],
                np.random.randn(*layer_buffers.inputs[key].shape))

    return layer_buffers
Example #2
0
def set_up_layer(layer, specs):
    layer.set_handler(HANDLER)
    time_steps = specs.get('time_steps', 3)
    batch_size = specs.get('batch_size', 2)
    hubs, layout = create_layout({'test_layer': layer})
    total_size, slices, shapes = get_total_size_slices_and_shapes(
        hubs, time_steps, batch_size)
    full_buffer = HANDLER.allocate((total_size, ))
    buffers = [full_buffer[slice_].reshape(shape)
               for slice_, shape in zip(slices, shapes)]
    view = create_buffer_views_from_layout(layout, buffers, hubs)

    layer_buffers = view.test_layer

    # init parameters randomly
    HANDLER.set_from_numpy(view.parameters,
                           np.random.randn(view.parameters.size) * 1)

    inits = specs.get('inits', {})
    for param_name, value in inits.items():
        HANDLER.set_from_numpy(view.test_layer.parameters[param_name], value)

    for key, value in view.test_layer.inputs.items():
        if key in specs:  # if a special input is given use that
            # print("Using special input:", key)
            HANDLER.set_from_numpy(layer_buffers.inputs[key], specs[key])
        else:  # otherwise randomize the input
            HANDLER.set_from_numpy(
                layer_buffers.inputs[key],
                np.random.randn(*layer_buffers.inputs[key].shape))

    return layer_buffers
Example #3
0
    def from_architecture(cls, architecture):
        """
        Create Network instance from given architecture.

        Args:
            architecture (dict):
                JSON serializable Architecture description.
        Returns:
            Network:
                A fully functional Network instance.
        """
        layers = instantiate_layers_from_architecture(architecture)
        hubs, layout = create_layout(layers)
        buffer_manager = BufferManager(layout, hubs)
        return cls(layers, buffer_manager, architecture)
Example #4
0
    def from_architecture(cls, architecture):
        """
        Create Network instance from given architecture.

        Args:
            architecture (dict):
                JSON serializable Architecture description.
        Returns:
            Network:
                A fully functional Network instance.
        """
        layers = instantiate_layers_from_architecture(architecture)
        hubs, layout = create_layout(layers)
        buffer_manager = BufferManager(layout, hubs)
        return cls(layers, buffer_manager, architecture)
Example #5
0
def test_create_layout(layers):
    hubs, layout = create_layout(layers)
    assert layout == {
        '@type': 'BufferView',
        'parameters': {
            '@type': 'array',
            '@index': 0,
            '@hub': 0,
            '@slice': (0, 230),
            '@shape': (230, ),
        },
        'gradients': {
            '@type': 'array',
            '@index': 1,
            '@hub': 4,
            '@slice': (0, 230),
            '@shape': (230, ),
            '@is_backward_only': True
        },
        'Input': {
            '@type': 'BufferView',
            '@index': 2,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 2),
                    '@hub': 1,
                    '@slice': (0, 2)
                },
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 2),
                    '@hub': 5,
                    '@slice': (0, 2),
                    '@is_backward_only': True
                },
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6
            },
        },
        'A': {
            '@type': 'BufferView',
            '@index': 3,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 2),
                    '@hub': 1,
                    '@slice': (0, 2)
                }
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 3),
                    '@hub': 2,
                    '@slice': (0, 3)
                }
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2,
                'W': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': (3, 2),
                    '@hub': 0,
                    '@slice': (0, 6)
                },
                'bias': {
                    '@type': 'array',
                    '@index': 1,
                    '@shape': (3, ),
                    '@hub': 0,
                    '@slice': (6, 9)
                }
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3,
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 2),
                    '@hub': 5,
                    '@slice': (0, 2),
                    '@is_backward_only': True
                }
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 3),
                    '@hub': 6,
                    '@slice': (0, 3),
                    '@is_backward_only': True
                }
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6,
                'W': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': (3, 2),
                    '@hub': 4,
                    '@slice': (0, 6),
                    '@is_backward_only': True
                },
                'bias': {
                    '@type': 'array',
                    '@index': 1,
                    '@shape': (3, ),
                    '@hub': 4,
                    '@slice': (6, 9),
                    '@is_backward_only': True
                }
            },
        },
        'B': {
            '@type': 'BufferView',
            '@index': 4,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 2),
                    '@hub': 1,
                    '@slice': (0, 2)
                }
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 5),
                    '@hub': 2,
                    '@slice': (3, 8)
                }
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2,
                'W': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': (5, 2),
                    '@hub': 0,
                    '@slice': (9, 19)
                },
                'bias': {
                    '@type': 'array',
                    '@index': 1,
                    '@shape': (5, ),
                    '@hub': 0,
                    '@slice': (19, 24)
                }
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3,
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 2),
                    '@hub': 5,
                    '@slice': (0, 2),
                    '@is_backward_only': True
                }
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 5),
                    '@hub': 6,
                    '@slice': (3, 8),
                    '@is_backward_only': True
                }
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6,
                'W': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': (5, 2),
                    '@hub': 4,
                    '@slice': (9, 19),
                    '@is_backward_only': True
                },
                'bias': {
                    '@type': 'array',
                    '@index': 1,
                    '@shape': (5, ),
                    '@hub': 4,
                    '@slice': (19, 24),
                    '@is_backward_only': True
                }
            },
        },
        'C': {
            '@type': 'BufferView',
            '@index': 5,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 8),
                    '@hub': 2,
                    '@slice': (0, 8)
                }
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 7),
                    '@hub': 2,
                    '@slice': (8, 15)
                }
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2,
                'W': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': (7, 8),
                    '@hub': 0,
                    '@slice': (24, 80)
                },
                'bias': {
                    '@type': 'array',
                    '@index': 1,
                    '@shape': (7, ),
                    '@hub': 0,
                    '@slice': (80, 87)
                }
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3,
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 8),
                    '@hub': 6,
                    '@slice': (0, 8),
                    '@is_backward_only': True
                }
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 7),
                    '@hub': 6,
                    '@slice': (8, 15),
                    '@is_backward_only': True
                }
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6,
                'W': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': (7, 8),
                    '@hub': 4,
                    '@slice': (24, 80),
                    '@is_backward_only': True
                },
                'bias': {
                    '@type': 'array',
                    '@index': 1,
                    '@shape': (7, ),
                    '@hub': 4,
                    '@slice': (80, 87),
                    '@is_backward_only': True
                }
            },
        },
        'D': {
            '@type': 'BufferView',
            '@index': 6,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 12),
                    '@hub': 2,
                    '@slice': (3, 15)
                }
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 11),
                    '@hub': 3,
                    '@slice': (0, 11)
                }
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2,
                'W': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': (11, 12),
                    '@hub': 0,
                    '@slice': (87, 219)
                },
                'bias': {
                    '@type': 'array',
                    '@index': 1,
                    '@shape': (11, ),
                    '@hub': 0,
                    '@slice': (219, 230)
                }
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3,
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 12),
                    '@hub': 6,
                    '@slice': (3, 15),
                    '@is_backward_only': True
                }
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': ('T', 'B', 11),
                    '@hub': 7,
                    '@slice': (0, 11),
                    '@is_backward_only': True
                }
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6,
                'W': {
                    '@type': 'array',
                    '@index': 0,
                    '@shape': (11, 12),
                    '@hub': 4,
                    '@slice': (87, 219),
                    '@is_backward_only': True
                },
                'bias': {
                    '@type': 'array',
                    '@index': 1,
                    '@shape': (11, ),
                    '@hub': 4,
                    '@slice': (219, 230),
                    '@is_backward_only': True
                }
            },
        }
    }
Example #6
0
def test_create_layout(layers):
    hubs, layout = create_layout(layers)
    assert layout == {
        '@type': 'BufferView',
        'parameters': {
            '@type': 'array',
            '@index': 0,
            '@hub': 0,
            '@slice': (0, 230),
            '@shape': (230, ),
        },
        'gradients': {
            '@type': 'array',
            '@index': 1,
            '@hub': 4,
            '@slice': (0, 230),
            '@shape': (230, ),
            '@is_backward_only': True
        },

        'Input': {
            '@type': 'BufferView',
            '@index': 2,
            'inputs': {'@type': 'BufferView', '@index': 0},
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 2),
                            '@hub': 1, '@slice': (0, 2)},
            },
            'parameters': {'@type': 'BufferView', '@index': 2},
            'internals': {'@type': 'BufferView', '@index': 3},
            'input_deltas': {'@type': 'BufferView', '@index': 4},
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 2),
                            '@hub': 5, '@slice': (0, 2),
                            '@is_backward_only': True},
            },
            'gradients': {'@type': 'BufferView', '@index': 6},
        },
        'A': {
            '@type': 'BufferView',
            '@index': 3,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 2),
                            '@hub': 1, '@slice': (0, 2)}
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 3),
                            '@hub': 2, '@slice': (0, 3)}
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2,
                'W': {'@type': 'array', '@index': 0, '@shape': (3, 2),
                      '@hub': 0, '@slice': (0, 6)},
                'bias': {'@type': 'array', '@index': 1, '@shape': (3,),
                         '@hub': 0, '@slice': (6, 9)}
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3,
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 2),
                            '@hub': 5, '@slice': (0, 2),
                            '@is_backward_only': True}
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 3),
                            '@hub': 6, '@slice': (0, 3),
                            '@is_backward_only': True}
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6,
                'W': {'@type': 'array', '@index': 0, '@shape': (3, 2),
                      '@hub': 4, '@slice': (0, 6),
                      '@is_backward_only': True},
                'bias': {'@type': 'array', '@index': 1, '@shape': (3,),
                         '@hub': 4, '@slice': (6, 9),
                         '@is_backward_only': True}
            },
        },
        'B': {
            '@type': 'BufferView',
            '@index': 4,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 2),
                            '@hub': 1, '@slice': (0, 2)}
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 5),
                            '@hub': 2, '@slice': (3, 8)}
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2,
                'W': {'@type': 'array', '@index': 0, '@shape': (5, 2),
                      '@hub': 0, '@slice': (9, 19)},
                'bias': {'@type': 'array', '@index': 1, '@shape': (5,),
                         '@hub': 0, '@slice': (19, 24)}
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3,
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 2),
                            '@hub': 5, '@slice': (0, 2),
                            '@is_backward_only': True}
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 5),
                            '@hub': 6, '@slice': (3, 8),
                            '@is_backward_only': True}
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6,
                'W': {'@type': 'array', '@index': 0, '@shape': (5, 2),
                      '@hub': 4, '@slice': (9, 19),
                      '@is_backward_only': True},
                'bias': {'@type': 'array', '@index': 1, '@shape': (5,),
                         '@hub': 4, '@slice': (19, 24),
                         '@is_backward_only': True}
            },
        },
        'C': {
            '@type': 'BufferView',
            '@index': 5,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 8),
                            '@hub': 2, '@slice': (0, 8)}
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 7),
                            '@hub': 2, '@slice': (8, 15)}
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2,
                'W': {'@type': 'array', '@index': 0, '@shape': (7, 8),
                      '@hub': 0, '@slice': (24, 80)},
                'bias': {'@type': 'array', '@index': 1, '@shape': (7,),
                         '@hub': 0, '@slice': (80, 87)}
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3,
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 8),
                            '@hub': 6, '@slice': (0, 8),
                            '@is_backward_only': True}
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 7),
                            '@hub': 6, '@slice': (8, 15),
                            '@is_backward_only': True}
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6,
                'W': {'@type': 'array', '@index': 0, '@shape': (7, 8),
                      '@hub': 4, '@slice': (24, 80),
                      '@is_backward_only': True},
                'bias': {'@type': 'array', '@index': 1, '@shape': (7,),
                         '@hub': 4, '@slice': (80, 87),
                         '@is_backward_only': True}
            },
        },
        'D': {
            '@type': 'BufferView',
            '@index': 6,
            'inputs': {
                '@type': 'BufferView',
                '@index': 0,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 12),
                            '@hub': 2, '@slice': (3, 15)}
            },
            'outputs': {
                '@type': 'BufferView',
                '@index': 1,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 11),
                            '@hub': 3, '@slice': (0, 11)}
            },
            'parameters': {
                '@type': 'BufferView',
                '@index': 2,
                'W': {'@type': 'array', '@index': 0, '@shape': (11, 12),
                      '@hub': 0, '@slice': (87, 219)},
                'bias': {'@type': 'array', '@index': 1, '@shape': (11,),
                         '@hub': 0, '@slice': (219, 230)}
            },
            'internals': {
                '@type': 'BufferView',
                '@index': 3,
            },
            'input_deltas': {
                '@type': 'BufferView',
                '@index': 4,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 12),
                            '@hub': 6, '@slice': (3, 15),
                            '@is_backward_only': True}
            },
            'output_deltas': {
                '@type': 'BufferView',
                '@index': 5,
                'default': {'@type': 'array', '@index': 0,
                            '@shape': ('T', 'B', 11),
                            '@hub': 7, '@slice': (0, 11),
                            '@is_backward_only': True}
            },
            'gradients': {
                '@type': 'BufferView',
                '@index': 6,
                'W': {'@type': 'array', '@index': 0, '@shape': (11, 12),
                      '@hub': 4, '@slice': (87, 219),
                      '@is_backward_only': True},
                'bias': {'@type': 'array', '@index': 1, '@shape': (11,),
                         '@hub': 4, '@slice': (219, 230),
                         '@is_backward_only': True}
            },
        }}