def test_chain_right_branch(model1, model2, model3): # Previously we 'flattened' these nested calls. We might opt to do so # again, especially for the operators. merge1 = chain(model1, model2) merge2 = chain(merge1, model3) assert len(merge1.layers) == 2 assert len(merge2.layers) == 2
def test_chain(ops): data = numpy.asarray([[1, 2, 3, 4]], dtype="f") model = chain(Linear(1), Dropout(), Linear(1)) model.ops = ops model.initialize(data, data) Y, backprop = model(data, is_train=True) backprop(Y) # Layers with and without nO/nI model = chain(Linear(1), Dropout(), Linear(1, 1)) model.initialize(data, data) # Setting dim on model model = chain(Linear(1), Dropout(), Linear(1)) model.set_dim("nO", 1) model.initialize(data, None) model = chain(Linear(1, 1), Dropout(), Linear(1, 1)) model.set_dim("nI", 1) model.initialize(None, data) # Not enough arguments with pytest.raises(TypeError): chain(Linear()) with pytest.raises(TypeError): chain()
def test_chain_two(model1, model2): model = chain(model1, model2) assert len(model.layers) == 2
def test_chain_one(model1): with pytest.raises(TypeError): chain(model1)
def test_chain_zero(): with pytest.raises(TypeError): chain()
def test_chain_three(model1, model2, model3): model = chain(model1, model2, model3) assert len(model.layers) == 3
from typing import List from thinc.layers import Relu, Softmax, chain, reduce_max, concatenate from thinc.model import Model # Define Custom X/Y types MyModelX = List[List[float]] MyModelY = List[List[float]] model: Model[MyModelX, MyModelY] = chain( Relu(12), Relu(12, dropout=0.2), Softmax(), ) # ERROR: incompatible type "bool", expected "List[List[float]]" model(False) # ERROR: List item 0 has incompatible type "str"; expected "float" model.begin_update([["0"]]) # ERROR: incompatible type "bool", expected "List[List[float]]" model.predict(True) # This example should be run with mypy. This is an example of type-level checking # for network validity. # # We first define an invalid network. # It's invalid because reduce_max expects Array3d as input, while Relu produces # Array2d as output. chain has type-logic to verify input and output types # line up. # # You should see the error an error, # examples/howto/type_chain.py:10: error: Cannot infer type argument 2 of "chain" bad_model = chain(Relu(10), reduce_max(), Softmax())