def test_add_edge_cases(): data = numpy.asarray([[1, 2, 3, 4]], dtype="f") with pytest.raises(TypeError): add() model = add(Linear(), Linear()) model._layers = [] Y, backprop = model(data, is_train=True) assert numpy.array_equal(data, Y) dX = backprop(Y) assert numpy.array_equal(dX, data)
def test_add(): data = numpy.asarray([[1, 2, 3, 4]], dtype="f") model = add(Linear(), Linear()) model.initialize(data, data) Y, backprop = model(data, is_train=True) Y2 = sum(layer.predict(data) for layer in model.layers) assert numpy.array_equal(Y, Y2) dX = backprop(Y) assert dX.shape == data.shape # Test that nesting works model2 = add(model, Linear()) assert len(model2.layers) == 3 model.initialize(data, data) Y = model2.predict(data) Y2 = sum(layer.predict(data) for layer in model2.layers) assert numpy.array_equal(Y, Y2)
from thinc.api import chain, ReLu, reduce_max, Softmax, add bad_model = chain(ReLu(10), reduce_max(), Softmax()) bad_model2 = add(ReLu(10), reduce_max(), Softmax())
from thinc.api import chain, Relu, reduce_max, Softmax, add good_model = chain(Relu(10), Relu(10), Softmax()) reveal_type(good_model) good_model2 = add(Relu(10), Relu(10), Softmax()) reveal_type(good_model2) bad_model_undetected = chain(Relu(10), Relu(10), reduce_max(), Softmax()) reveal_type(bad_model_undetected) bad_model_undetected2 = add(Relu(10), Relu(10), reduce_max(), Softmax()) reveal_type(bad_model_undetected2)
from thinc.api import chain, Relu, reduce_max, Softmax, add, concatenate bad_model = chain(Relu(10), reduce_max(), Softmax()) bad_model2 = add(Relu(10), reduce_max(), Softmax()) bad_model_only_plugin = chain(Relu(10), Relu(10), Relu(10), Relu(10), reduce_max(), Softmax()) bad_model_only_plugin2 = add(Relu(10), Relu(10), Relu(10), Relu(10), reduce_max(), Softmax()) reveal_type(bad_model_only_plugin2) bad_model_only_plugin3 = concatenate(Relu(10), Relu(10), Relu(10), Relu(10), reduce_max(), Softmax()) reveal_type(bad_model_only_plugin3)
from typing import Any, TypeVar from thinc.api import chain, ReLu, reduce_max, Softmax, add, Model good_model = chain(ReLu(10), ReLu(10), Softmax()) reveal_type(good_model) good_model2 = add(ReLu(10), ReLu(10), Softmax()) reveal_type(good_model2) bad_model_undetected = chain(ReLu(10), ReLu(10), ReLu(10), ReLu(10), Softmax()) reveal_type(bad_model_undetected) bad_model_undetected2 = add(ReLu(10), ReLu(10), ReLu(10), ReLu(10), Softmax()) reveal_type(bad_model_undetected2) def forward() -> None: pass OtherType = TypeVar("OtherType") def other_function(layer1: Model, layer2: Model, *layers: Model) -> Model[Any, OtherType]: return Model("some_model", forward) non_combinator_model = other_function(Model("x", forward), Model("y", forward), Model("z", forward))