def test_generate_architecture(): l1 = ConstructionWrapper.create('InputLayerImpl') l2 = ConstructionWrapper.create('FooLayerImpl', name='bar') l3 = ConstructionWrapper.create('FooLayerImpl', name='baz') l4 = ConstructionWrapper.create('FooLayerImpl', name='out') _ = l1 - 'foo' >> l2 >> 'A' - l4 _ = l1 - 'bar' >> l3 >> 'B' - l4 arch1 = generate_architecture(l1.layer) arch2 = generate_architecture(l2.layer) arch3 = generate_architecture(l3.layer) assert arch1 == arch2 assert arch1 == arch3 assert arch1 == { 'Input': { '@type': 'Input', '@outgoing_connections': { 'foo': ['bar.default'], 'bar': ['baz.default'], } }, 'bar': { '@type': 'Foo', '@outgoing_connections': { 'default': ['out.A'], } }, 'baz': { '@type': 'Foo', '@outgoing_connections': { 'default': ['out.B'], } }, 'out': { '@type': 'Foo', '@outgoing_connections': {} } }
def from_layer(cls, some_layer): """ Create Network instance from a construction layer. Args: some_layer (brainstorm.construction.ConstructionWrapper): Some layer used to wire up an architecture with `>>` Returns: Network: A fully functional Network instance. """ arch = generate_architecture(some_layer) return cls.from_architecture(arch)
assert isinstance(pch2, PyCudaHandler) def test_describe_numpy_handler(): nh = NumpyHandler(np.float32) d = get_description(nh) assert d == {'@type': 'NumpyHandler', 'dtype': 'float32'} nh2 = create_from_description(d) assert isinstance(nh2, NumpyHandler) assert nh2.dtype == np.float32 # ################# test describing a Network ################################# arch = generate_architecture( bs.layers.Input(out_shapes={'default': ( 'T', 'B', 7)}) >> bs.layers.FullyConnected(3) >> bs.layers.Loss()) def test_describe_network(): net = bs.Network.from_architecture(arch) net.initialize(1) assert get_description(net) == { '@type': 'Network', 'architecture': json.loads(json.dumps(arch)), 'handler': { '@type': 'NumpyHandler', 'dtype': 'float32' }, 'initializers': { 'default': 1
pch2 = create_from_description(d) assert isinstance(pch2, PyCudaHandler) def test_describe_numpy_handler(): nh = NumpyHandler(np.float32) d = get_description(nh) assert d == {'@type': 'NumpyHandler', 'dtype': 'float32'} nh2 = create_from_description(d) assert isinstance(nh2, NumpyHandler) assert nh2.dtype == np.float32 # ################# test describing a Network ################################# arch = generate_architecture( bs.layers.Input(out_shapes={'default': ('T', 'B', 7)}) >> bs.layers.FullyConnected(3) >> bs.layers.Loss()) def test_describe_network(): net = bs.Network.from_architecture(arch) net.initialize(1) assert get_description(net) == { '@type': 'Network', 'architecture': json.loads(json.dumps(arch)), 'handler': {'@type': 'NumpyHandler', 'dtype': 'float32'}, 'initializers': {'default': 1}, 'weight_modifiers': {}, 'gradient_modifiers': {}, 'output_name': None }