1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
import numpy
import pytest
from numpy.testing import assert_allclose
from thinc.api import (
Dropout,
Linear,
Model,
NumpyOps,
add,
clone,
concatenate,
map_list,
noop,
)
from thinc.layers import chain, tuplify
@pytest.fixture(params=[1, 2, 9])
def nB(request):
return request.param
@pytest.fixture(params=[1, 6])
def nI(request):
return request.param
@pytest.fixture(params=[1, 5, 3])
def nH(request):
return request.param
@pytest.fixture(params=[1, 2, 7, 9])
def nO(request):
return request.param
@pytest.fixture
def model1(nH, nI):
return Linear(nH, nI)
@pytest.fixture
def model2(nO, nH):
return Linear(nO, nH)
@pytest.fixture
def model3(nO):
return Linear(nO, nO)
def test_tuplify_zero():
with pytest.raises(TypeError):
tuplify()
def test_tuplify_one(model1):
with pytest.raises(TypeError):
tuplify(model1)
def test_tuplify_two(model1, model2):
model = tuplify(model1, model2)
assert len(model.layers) == 2
def test_tuplify_operator_two(model1, model2):
with Model.define_operators({"&": tuplify}):
model = model1 & model2
assert len(model.layers) == 2
def test_tuplify_dulicates_input():
model = tuplify(noop(), noop())
ones = numpy.ones([10])
out = model.predict(ones)
assert out == (ones, ones)
def test_tuplify_initialize(nI, nO):
linear = Linear(nO)
model = tuplify(linear, linear)
ones = numpy.ones((1, nI), dtype="float")
model.initialize(X=ones)
def test_tuplify_three(model1, model2, model3):
model = tuplify(model1, model2, model3)
assert len(model.layers) == 3
def test_tuplify_operator_three(model1, model2, model3):
# Previously we 'flattened' these nested calls. We might opt to do so
# again, especially for the operators.
with Model.define_operators({"&": tuplify}):
model = model1 & model2 & model3
assert len(model.layers) == 2
assert len(model.layers[0].layers) == 2
def test_chain_zero():
with pytest.raises(TypeError):
chain()
def test_chain_one(model1):
with pytest.raises(TypeError):
chain(model1)
def test_chain_two(model1, model2):
model = chain(model1, model2)
assert len(model.layers) == 2
def test_chain_operator_two(model1, model2):
with Model.define_operators({">>": chain}):
model = model1 >> model2
assert len(model.layers) == 2
def test_chain_three(model1, model2, model3):
model = chain(model1, model2, model3)
assert len(model.layers) == 3
def test_chain_operator_three(model1, model2, model3):
# Previously we 'flattened' these nested calls. We might opt to do so
# again, especially for the operators.
with Model.define_operators({">>": chain}):
model = model1 >> model2 >> model3
assert len(model.layers) == 2
assert len(model.layers[0].layers) == 2
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
@pytest.mark.parametrize("ops", [NumpyOps(), NumpyOps(use_blis=True)])
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_concatenate_one(model1):
model = concatenate(model1)
assert isinstance(model, Model)
def test_concatenate_two(model1, model2):
model = concatenate(model1, model2)
assert len(model.layers) == 2
def test_concatenate_operator_two(model1, model2):
with Model.define_operators({"|": concatenate}):
model = model1 | model2
assert len(model.layers) == 2
def test_concatenate_three(model1, model2, model3):
model = concatenate(model1, model2, model3)
assert len(model.layers) == 3
def test_concatenate_operator_three(model1, model2, model3):
with Model.define_operators({"|": concatenate}):
model = model1 | model2 | model3
assert len(model.layers) == 3
def test_clone_changes_predictions(nH, nI):
model1 = Linear(nH)
model = clone(model1, 10)
ones = numpy.ones((10, nI), dtype="f")
model.initialize(X=ones)
output_from_cloned = model.predict(ones)
output_from_orig = model1.predict(ones)
assert output_from_cloned.sum() != output_from_orig.sum()
def test_clone_gives_distinct_ids(nH, nI):
model = clone(Linear(nH), 5)
assert len(model.layers) == 5
seen_ids = set()
for node in model.walk():
assert node.id not in seen_ids
seen_ids.add(node.id)
assert len(seen_ids) == 6
def test_clone_noop():
model = clone(Linear(), 0)
assert len(model.layers) == 0
assert model.name == "noop"
def test_concatenate_noop():
model = concatenate()
assert len(model.layers) == 0
assert model.name == "noop"
def test_noop():
data = numpy.asarray([1, 2, 3], dtype="f")
model = noop(Linear(), Linear())
model.initialize(data, data)
Y, backprop = model(data, is_train=True)
assert numpy.array_equal(Y, data)
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)
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_concatenate():
data = numpy.asarray([[1, 2, 3], [4, 5, 6]], dtype="f")
model = concatenate(Linear(), Linear())
model.initialize(data, data)
Y, backprop = model(data, is_train=True)
assert Y.shape[1] == sum([layer.predict(data).shape[1] for layer in model.layers])
dX = backprop(Y)
assert dX.shape == data.shape
def test_map_list():
nI = 4
nO = 9
Xs = [numpy.zeros((6, nI), dtype="f"), numpy.ones((3, nI), dtype="f")]
Y_shapes = [(x.shape[0], nO) for x in Xs]
model = map_list(Linear())
model.initialize(X=Xs, Y=[numpy.zeros(shape, dtype="f") for shape in Y_shapes])
Ys, backprop = model(Xs, is_train=True)
assert isinstance(Ys, list)
assert len(Ys) == len(Xs)
layer = model.layers[0]
for X, Y in zip(Xs, Ys):
assert_allclose(layer.predict(X), Y)
dXs = backprop(Ys)
assert isinstance(dXs, list)
assert len(dXs) == len(Xs)
assert dXs[0].shape == Xs[0].shape
assert dXs[1].shape == Xs[1].shape
|