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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
|
---
title: Backends & Math
next: /docs/api-util
---
All Thinc models have a reference to an `Ops` instance, that provides access to
**memory allocation** and **mathematical routines**. The `Model.ops` instance
also keeps track of state and settings, so that you can have different models in
your network executing on different devices or delegating to different
underlying libraries.
Each `Ops` instance holds a reference to a numpy-like module (`numpy` or
`cupy`), which you can access at `Model.ops.xp`. This is enough to make most
layers work on **both CPU and GPU devices**. Additionally, there are several
routines that we have implemented as methods on the `Ops` object, so that
specialized versions can be called for different backends. You can also create
your own `Ops` subclasses with specialized routines for your layers, and use the
[`set_current_ops`](#set_current_ops) function to change the default.
| Backend | CPU | GPU | TPU | Description |
| ---------- | :----------------: | :----------------: | :---------------: | ----------------------------------------------------------------------------------------------------------- |
| `AppleOps` | <i name="yes"></i> | <i name="no"></i> | <i name="no"></i> | Use AMX matrix multiplication units on Apple Silicon Macs. Added in Thinc 9.0. |
| `CupyOps` | <i name="no"></i> | <i name="yes"></i> | <i name="no"></i> | Execute via [`cupy`](https://cupy.chainer.org/) and custom CUDA. |
| `MPSOps` | <i name="yes"></i> | <i name="yes"></i> | <i name="no"></i> | Use the GPU on Apple Silicon Macs for PyTorch models, use AMX matrix multiplication units for Thinc Models. |
| `NumpyOps` | <i name="yes"></i> | <i name="no"></i> | <i name="no"></i> | Execute via `numpy`, [`blis`](https://github.com/explosion/cython-blis) (optional) and custom Cython. |
## Ops {#ops tag="class"}
The `Ops` class is typically not used directly but via `NumpyOps`, `AppleOps`,
`CupyOps` or `MPSOps`, which are subclasses of `Ops` and implement a **more
efficient subset of the methods**. You also have access to the ops via the
[`Model.ops`](/docs/api-model#attributes) attribute. The documented methods
below list which backends provide optimized and more efficient versions
(indicated by <i name="yes"></i>), and which use the default implementation.
Thinc also provides various [helper functions](#util) for getting and setting
different backends.
<infobox variant="warning">
The current set of implemented methods is somewhat arbitrary and **subject to
change**. Methods are moved to the `Ops` object if we want different
implementations for different backends, e.g. cythonized CPU versions or custom
CUDA kernels.
</infobox>
```python
### Example
from thinc.api import Linear, get_ops, use_ops
model = Linear(4, 2)
X = model.ops.alloc2f(10, 2)
blis_ops = get_ops("numpy", use_blis=True)
use_ops(blis_ops)
```
### Attributes {#attributes}
| Name | Type | Description |
| ------------- | ------------ | ---------------------------------------------------------------------------------------- |
| `name` | <tt>str</tt> | **Class attribute:** Backend name, `"numpy"`, `"apple"`, `"cupy"` or `"mps"`. |
| `xp` | <tt>Xp</tt> | **Class attribute:** `numpy` or `cupy`. |
| `device_type` | <tt>str</tt> | The device type to use, if available for the given backend: `"cpu"`, `"gpu"` or `"tpu"`. |
| `device_id` | <tt>int</tt> | The device ID to use, if available for the given backend. |
### Ops.\_\_init\_\_ {#init tag="method"}
| Argument | Type | Description |
| -------------- | ------------- | ------------------------------------------------------------------------------------------------------------- |
| `device_type` | <tt>str</tt> | The device type to use, if available for the given backend: `"cpu"`, `"gpu"` or `"tpu"`. |
| `device_id` | <tt>int</tt> | The device ID to use, if available for the given backend. |
| _keyword-only_ | | |
| `use_blis` | <tt>bool</tt> | `NumpyOps`: Use [`blis`](https://github.com/explosion/cython-blis) for single-threaded matrix multiplication. |
### Ops.minibatch {#minibatch tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Iterate slices from a sequence, optionally shuffled. Slices may be either views
or copies of the underlying data. Supports the batchable data types
[`Pairs`](/docs/api-types#pairs), [`Ragged`](/docs/api-types#ragged) and
[`Padded`](/docs/api-types#padded), as well as arrays, lists and tuples. The
`size` argument may be either an integer, or a sequence of integers. If a
sequence, a new size is drawn before every output. If `shuffle` is `True`,
shuffled batches are produced by first generating an index array, shuffling it,
and then using it to slice into the sequence. An internal queue of `buffer`
items is accumulated before being each output. Buffering is useful for some
devices, to allow the network to run asynchronously without blocking on every
batch.
The method returns a [`SizedGenerator`](/docs/api-types#sizedgenerator) that
exposes a `__len__` and is rebatched and reshuffled every time it's executed,
allowing you to move the batching outside of the training loop.
```python
### Example
batches = model.ops.minibatch(128, train_X, shuffle=True)
```
| Argument | Type | Description |
| -------------- | ------------------------------ | ------------------------------------------------------------------ |
| `size` | <tt>Union[int, Generator]</tt> | The batch size(s). |
| `sequence` | <tt>Batchable</tt> | The sequence to batch. |
| _keyword-only_ | | |
| `shuffle` | <tt>bool</tt> | Whether to shuffle the items. |
| `buffer` | <tt>int</tt> | Number of items to accumulate before each output. Defaults to `1`. |
| **RETURNS** | <tt>SizedGenerator</tt> | The batched items. |
### Ops.multibatch {#multibatch tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Minibatch one or more sequences of data, and return lists with one batch per
sequence. Otherwise identical to [`Ops.minibatch`](#minibatch).
```python
### Example
batches = model.ops.multibatch(128, train_X, train_Y, shuffle=True)
```
| Argument | Type | Description |
| -------------- | ------------------------------ | ------------------------------------------------------------------ |
| `size` | <tt>Union[int, Generator]</tt> | The batch size(s). |
| `sequence` | <tt>Batchable</tt> | The sequence to batch. |
| `*other` | <tt>Batchable</tt> | The other sequences to batch. |
| _keyword-only_ | | |
| `shuffle` | <tt>bool</tt> | Whether to shuffle the items. |
| `buffer` | <tt>int</tt> | Number of items to accumulate before each output. Defaults to `1`. |
| **RETURNS** | <tt>SizedGenerator</tt> | The batched items. |
### Ops.seq2col {#seq2col tag="method"}
<inline-list>
- **default:** <i name="yes"></i> (`nW=1` only)
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Given an `(M, N)` sequence of vectors, return an `(M, N*(nW*2+1))` sequence. The
new sequence is constructed by concatenating `nW` preceding and succeeding
vectors onto each column in the sequence, to extract a window of features.
| Argument | Type | Description |
| -------------- | ------------------------- | ----------------------------------------------------------------- |
| `seq` | <tt>Floats2d</tt> | The original sequence. |
| `nW` | <tt>int</tt> | The window size. |
| _keyword-only_ | | |
| `lengths` | <tt>Optional[Ints1d]</tt> | Sequence lengths, introduces padding around sequences. |
| **RETURNS** | <tt>Floats2d</tt> | The created sequence containing preceding and succeeding vectors. |
### Ops.backprop_seq2col {#backprop_seq2col tag="method"}
<inline-list>
- **default:** <i name="yes"></i> (`nW=1` only)
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
The reverse/backward operation of the `seq2col` function: calculate the gradient
of the original `(M, N)` sequence, as a function of the gradient of the output
`(M, N*(nW*2+1))` sequence.
| Argument | Type | Description |
| -------------- | ------------------------- | ------------------------------------------------------ |
| `dY` | <tt>Floats2d</tt> | Gradient of the output sequence. |
| `nW` | <tt>int</tt> | The window size. |
| _keyword-only_ | | |
| `lengths` | <tt>Optional[Ints1d]</tt> | Sequence lengths, introduces padding around sequences. |
| **RETURNS** | <tt>Floats2d</tt> | Gradient of the original sequence. |
### Ops.gemm {#gemm tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Perform General Matrix Multiplication (GeMM) and optionally store the result in
the specified output variable.
| Argument | Type | Description |
| ----------- | --------------------------- | ------------------------------------------------------------- |
| `x` | <tt>Floats2d</tt> | First array. |
| `y` | <tt>Floats2d</tt> | Second array. |
| `out` | <tt>Optional[Floats2d]</tt> | Variable to store the result of the matrix multiplication in. |
| `trans1` | <tt>bool</tt> | Whether or not to transpose array `x`. |
| `trans2` | <tt>bool</tt> | Whether or not to transpose array `y`. |
| **RETURNS** | <tt>Floats2d</tt> | The result of the matrix multiplication. |
### Ops.affine {#affine tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Apply a weights layer and a bias to some inputs, i.e. `Y = X @ W.T + b`.
| Argument | Type | Description |
| ----------- | ----------------- | ---------------- |
| `X` | <tt>Floats2d</tt> | The inputs. |
| `W` | <tt>Floats2d</tt> | The weights. |
| `b` | <tt>Floats1d</tt> | The bias vector. |
| **RETURNS** | <tt>Floats2d</tt> | The output. |
### Ops.flatten {#flatten tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Flatten a list of arrays into one large array.
| Argument | Type | Description |
| --------------- | -------------------------- | ------------------------------------------------------------- |
| `X` | <tt>Sequence[ArrayXd]</tt> | The original list of arrays. |
| `dtype` | <tt>Optional[DTypes]</tt> | The data type to cast the resulting array in. |
| `pad` | <tt>int</tt> | The number of zeros to add as padding to `X` (default 0). |
| `ndim_if_empty` | <tt>int</tt> | The dimension of the output result if `X` is `None` or empty. |
| **RETURNS** | <tt>ArrayXd</tt> | One large array storing all original information. |
### Ops.unflatten {#unflatten tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
The reverse/backward operation of the `flatten` function: unflatten a large
array into a list of arrays according to the given lengths.
| Argument | Type | Description |
| ----------- | ---------------------- | --------------------------------------------------------------------- |
| `X` | <tt>ArrayXd</tt> | The flattened array. |
| `lengths` | <tt>Ints1d</tt> | The lengths of the original arrays before they were flattened. |
| `pad` | <tt>int</tt> | The padding that was applied during the `flatten` step (default 0). |
| **RETURNS** | <tt>List[ArrayXd]</tt> | A list of arrays storing the same information as the flattened array. |
### Ops.pad {#pad tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Perform padding on a list of arrays so that they each have the same length, by
taking the maximum dimension across each axis. This only works on non-empty
sequences with the same `ndim` and `dtype`.
| Argument | Type | Description |
| ----------- | ---------------------- | ------------------------------------------------------------------------------------------------ |
| `seqs` | <tt>List[Array2d]</tt> | The sequences to pad. |
| `round_to` | <tt>int</tt> | Round the length to nearest bucket (helps on GPU, to make similar array sizes). Defaults to `1`. |
| **RETURNS** | <tt>Array3d</tt> | The padded sequences, stored in one array. |
### Ops.unpad {#unpad tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
The reverse/backward operation of the `pad` function: transform an array back
into a list of arrays, each with their original length.
| Argument | Type | Description |
| ----------- | ---------------------- | ----------------------------------------------- |
| `padded` | <tt>ArrayXd</tt> | The padded sequences, stored in one array. |
| `lengths` | <tt>List[int]</tt> | The original lengths of the unpadded sequences. |
| **RETURNS** | <tt>List[ArrayXd]</tt> | The unpadded sequences. |
### Ops.list2padded {#list2padded tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Pack a sequence of two-dimensional arrays into a
[`Padded`](/docs/api-types#padded) datatype.
| Argument | Type | Description |
| ----------- | ---------------------- | ---------------------- |
| `seqs` | <tt>List[Array2d]</tt> | The sequences to pack. |
| **RETURNS** | <tt>Padded</tt> | The packed arrays. |
### Ops.padded2list {#padded2list tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Unpack a [`Padded`](/docs/api-types#padded) datatype to a list of
two-dimensional arrays.
| Argument | Type | Description |
| ----------- | ---------------------- | ----------------------- |
| `padded` | <tt>Padded</tt> | The object to unpack. |
| **RETURNS** | <tt>List[Array2d]</tt> | The unpacked sequences. |
### Ops.get_dropout_mask {#get_dropout_mask tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Create a random mask for applying dropout, with a certain percent of the mask
(defined by `drop`) will contain zeros. The neurons at those positions will be
deactivated during training, resulting in a more robust network and less
overfitting.
| Argument | Type | Description |
| ----------- | ------------------------ | ----------------------------------------------------------- |
| `shape` | <tt>Shape</tt> | The input shape. |
| `drop` | <tt>Optional[float]</tt> | The dropout rate. |
| **RETURNS** | <tt>Floats</tt> | A mask specifying a 0 where a neuron should be deactivated. |
### Ops.alloc {#alloc tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** default
</inline-list>
Allocate an array of a certain shape. If possible, you should always use the
**type-specific methods** listed below, as they make the code more readable and
allow more sophisticated static [type checking](/docs/usage-type-checking) of
the inputs and outputs.
| Argument | Type | Description |
| -------------- | ---------------- | -------------------------------------------- |
| `shape` | <tt>Shape</tt> | The shape. |
| _keyword-only_ | | |
| `dtype` | <tt>DTypes</tt> | The data type (default: `float32`). |
| `zeros` | <tt>bool</tt> | Fill the array with zeros (default: `True`). |
| **RETURNS** | <tt>ArrayXd</tt> | An array of the correct shape and data type. |
### Ops.cblas {#cblas tag="method"}
<inline-list>
- **default:** <i name="no"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="no"></i>
</inline-list>
Get a table of C BLAS functions usable in Cython `cdef nogil` functions. This
method does not take any arguments.
<infobox variant="warning">
This method is only supported by `NumpyOps`. A `NotImplementedError` exception
is raised when calling this method on `Ops` or `CupyOps`.
</infobox>
### Ops.to_numpy {#to_numpy tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** <i name="yes"></i>
</inline-list>
Convert the array to a numpy array.
| Argument | Type | Description |
| -------------- | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data` | <tt>ArrayXd</tt> | The array. |
| _keyword-only_ | | |
| `byte_order` | <tt>Optional[str]</tt> | The [new byte order](https://numpy.org/doc/stable/reference/generated/numpy.dtype.newbyteorder.html), `None` preserves the current byte order (default: `None`). |
| **RETURNS** | <tt>numpy.ndarray</tt> | A numpy array with the specified byte order. |
#### Type-specific methods
<inline-list>
- **Floats:** `Ops.alloc_f`, `Ops.alloc1f`, `Ops.alloc2f`, `Ops.alloc3f`,
`Ops.alloc4f`
- **Ints:** `Ops.alloc_i`, `Ops.alloc1i`, `Ops.alloc2i`, `Ops.alloc3i`,
`Ops.alloc4i`
</inline-list>
Shortcuts to allocate an array of a certain shape and data type (`f` refers to
`float32` and `i` to `int32`). For instance, `Ops.alloc2f` will allocate an
two-dimensional array of floats.
```python
### Example
X = model.ops.alloc2f(10, 2) # Floats2d
Y = model.ops.alloc1i(4) # Ints1d
```
| Argument | Type | Description |
| -------------- | ----------------------------------------- | -------------------------------------------------------------------------- |
| `*shape` | <tt>int</tt> | The shape, one positional argument per dimension. |
| _keyword-only_ | | |
| `dtype` | <tt>DTypesInt</tt> / <tt>DTypesFloat</tt> | The data type (float type for float methods and int type for int methods). |
| `zeros` | <tt>bool</tt> | Fill the array with zeros (default: `True`). |
| **RETURNS** | <tt>ArrayXd</tt> | An array of the correct shape and data type. |
### Ops.reshape {#reshape tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Reshape an array and return an array containing the same data with the given
shape. If possible, you should always use the **type-specific methods** listed
below, as they make the code more readable and allow more sophisticated static
[type checking](/docs/usage-type-checking) of the inputs and outputs.
| Argument | Type | Description |
| ----------- | ---------------- | --------------------- |
| `array` | <tt>ArrayXd</tt> | The array to reshape. |
| `shape` | <tt>Shape</tt> | The shape. |
| **RETURNS** | <tt>ArrayXd</tt> | The reshaped array. |
#### Type-specific methods
<inline-list>
- **Floats:** `Ops.reshape_f`, `Ops.reshape1f`, `Ops.reshape2f`,
`Ops.reshape3f`, `Ops.reshape4f`
- **Ints:** `Ops.reshape_i`, `Ops.reshape1i`, `Ops.reshape2i`, `Ops.reshape3i`,
`Ops.reshape4i`
</inline-list>
Shortcuts to reshape an array of a certain shape and data type (`f` refers to
`float32` and `i` to `int32`). For instance, `reshape2f` can be used to reshape
an array of floats to a 2d-array of floats.
<infobox variant="warning">
Note that the data type-specific methods mostly exist for **static type checking
purposes**. They do **not** change the data type of the array. For example,
`Ops.reshape2f` expects an array of floats and expects to return an array of
floats – but it won't convert an array of ints to an array of floats. However,
using the specific method will tell the static type checker what array to
expect, and passing in an array that's _typed_ as an int array will result in a
type error.
</infobox>
```python
### Example {small="true"}
X = model.ops.reshape2f(X, 10, 2) # Floats2d
Y = model.ops.reshape1i(Y, 4) # Ints1d
```
| Argument | Type | Description |
| ----------- | ---------------- | -------------------------------------------------------------- |
| `array` | <tt>ArrayXd</tt> | The array to reshape (of the same data type). |
| `*shape` | <tt>int</tt> | The shape, one positional argument per dimension. |
| **RETURNS** | <tt>ArrayXd</tt> | The reshaped array (of the same data type as the input array). |
### Ops.asarray {#asarray tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Ensure a given array is of the correct type, e.g. `numpy.ndarray` for `NumpyOps`
or `cupy.ndarray` for `CupyOps`. If possible, you should always use the
**type-specific methods** listed below, as they make the code more readable and
allow more sophisticated static [type checking](/docs/usage-type-checking) of
the inputs and outputs.
| Argument | Type | Description |
| -------------- | --------------------------------------------------------- | ------------------------------------------ |
| `data` | <tt>Union[ArrayXd, Sequence[ArrayXd], Sequence[int]]</tt> | The original array. |
| _keyword-only_ | | |
| `dtype` | <tt>Optional[DTypes]</tt> | The data type |
| **RETURNS** | <tt>ArrayXd</tt> | The array transformed to the correct type. |
### Type-specific methods
<inline-list>
- **Floats:** `Ops.asarray_f`, `Ops.asarray1f`, `Ops.asarray2f`,
`Ops.asarray3f`, `Ops.asarray4f`
- **Ints:** `Ops.asarray_i`, `Ops.asarray1i`, `Ops.asarray2i`, `Ops.asarray3i`,
`Ops.asarray4i`
</inline-list>
Shortcuts for specific dimensions and data types (`f` refers to `float32` and
`i` to `int32`). For instance, `Ops.asarray2f` will return a two-dimensional
array of floats.
```python
### Example
X = model.ops.asarray2f(X, 10, 2) # Floats2d
Y = model.ops.asarray1i(Y, 4) # Ints1d
```
| Argument | Type | Description |
| -------------- | ----------------------------------------- | -------------------------------------------------------------------------- |
| `*shape` | <tt>int</tt> | The shape, one positional argument per dimension. |
| _keyword-only_ | | |
| `dtype` | <tt>DTypesInt</tt> / <tt>DTypesFloat</tt> | The data type (float type for float methods and int type for int methods). |
| **RETURNS** | <tt>ArrayXd</tt> | An array of the correct shape and data type, filled with zeros. |
### Ops.as_contig {#as_contig tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Allow the backend to make a contiguous copy of an array. Implementations of
`Ops` do not have to make a copy or make it contiguous if that would not improve
efficiency for the execution engine.
| Argument | Type | Description |
| -------------- | ------------------------- | --------------------------------------------- |
| `data` | <tt>ArrayXd</tt> | The array. |
| _keyword-only_ | | |
| `dtype` | <tt>Optional[DTypes]</tt> | The data type |
| **RETURNS** | <tt>ArrayXd</tt> | An array with the same contents as the input. |
### Ops.unzip {#unzip tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Unzip a tuple of two arrays, transform them with `asarray` and return them as
two separate arrays.
| Argument | Type | Description |
| ----------- | -------------------------------- | ------------------------------------------- |
| `data` | <tt>Tuple[ArrayXd, ArrayXd] | The tuple of two arrays. |
| **RETURNS** | <tt>Tuple[ArrayXd, ArrayXd]</tt> | The two arrays, transformed with `asarray`. |
### Ops.sigmoid {#sigmoid tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Calculate the sigmoid function.
| Argument | Type | Description |
| -------------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The input values. |
| _keyword-only_ | | |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The output values, i.e. `S(X)`. |
### Ops.dsigmoid {#dsigmoid tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Calculate the derivative of the `sigmoid` function.
| Argument | Type | Description |
| -------------- | ----------------- | ------------------------------------------ |
| `Y` | <tt>FloatsXd</tt> | The input values. |
| _keyword-only_ | | |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The output values, i.e. `dS(Y)`. |
### Ops.dtanh {#dtanh tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Calculate the derivative of the `tanh` function.
| Argument | Type | Description |
| -------------- | ----------------- | ------------------------------------------ |
| `Y` | <tt>FloatsXd</tt> | The input values. |
| _keyword-only_ | | |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The output values, i.e. `dtanh(Y)`. |
### Ops.softmax {#softmax tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Calculate the softmax function. The resulting array will sum up to 1.
| Argument | Type | Description |
| -------------- | ----------------- | ------------------------------------------------------ |
| `x` | <tt>FloatsXd</tt> | The input values. |
| _keyword-only_ | | |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| `axis` | <tt>int</tt> | The dimension to normalize over. |
| `temperature` | <tt>float</tt> | The value to divide the unnormalized probabilities by. |
| **RETURNS** | <tt>FloatsXd</tt> | The normalized output values. |
### Ops.backprop_softmax {#backprop_softmax tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
| Argument | Type | Description |
| -------------- | ----------------- | ------------------------------------------------------ |
| `Y` | <tt>FloatsXd</tt> | Output array. |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| _keyword-only_ | | |
| `axis` | <tt>int</tt> | The dimension that was normalized over. |
| `temperature` | <tt>float</tt> | The value to divide the unnormalized probabilities by. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradients of the input array. |
### Ops.softmax_sequences {#softmax_sequences tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
| Argument | Type | Description |
| -------------- | ----------------- | ------------------------------------------ |
| `Xs` | <tt>Floats2d</tt> | An 2d array of input sequences. |
| `lengths` | <tt>Ints1d</tt> | The lengths of the input sequences. |
| _keyword-only_ | | |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| `axis` | <tt>int</tt> | The dimension to normalize over. |
| **RETURNS** | <tt>Floats2d</tt> | The normalized output values. |
### Ops.backprop_softmax_sequences {#backprop_softmax_sequences tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
The reverse/backward operation of the `softmax` function.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------- |
| `dY` | <tt>Floats2d</tt> | Gradients of the output array. |
| `Y` | <tt>Floats2d</tt> | Output array. |
| `lengths` | <tt>Ints1d</tt> | The lengths of the input sequences. |
| **RETURNS** | <tt>Floats2d</tt> | The gradients of the input sequences. |
### Ops.recurrent_lstm {#recurrent_lstm tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Encode a padded batch of inputs into a padded batch of outputs using an LSTM.
| Argument | Type | Description |
| ----------- | ------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
| `W` | <tt>Floats2d</tt> | The weights, shaped `(nO * 4, nO + nI)`. |
| `b` | <tt>Floats1d</tt> | The bias vector, shaped `(nO * 4,)`. |
| `h_init` | <tt>Floats1d</tt> | Initial value for the previous hidden vector. |
| `c_init` | <tt>Floats1d</tt> | Initial value for the previous cell state. |
| `inputs` | <tt>Floats3d</tt> | A batch of inputs, shaped `(nL, nB, nI)`, where `nL` is the sequence length and `nB` is the batch size. |
| `is_train` | <tt>bool</tt> | Whether the model is running in a training context. |
| **RETURNS** | <tt>Tuple[Floats3d, Tuple[Floats3d, Floats3d, Floats3d]]</tt> | A tuple consisting of the outputs and the intermediate activations required for the backward pass. The outputs are shaped `(nL, nB, nO)`. |
### Ops.backprop_recurrent_lstm {#backprop_recurrent_lstm tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Compute the gradients for the `recurrent_lstm` operation via backpropagation.
| Argument | Type | Description |
| ----------- | ----------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| `dY` | <tt>Floats3d</tt> | The gradient w.r.t. the outputs. |
| `fwd_state` | <tt>Tuple[Floats3d, Floats3d, Floats3d]</tt> | The tuple of gates, cells and inputs, returned by the forward pass. |
| `params` | <tt>Tuple[Floats2d, Floats1d]</tt> | A tuple of the weights and biases. |
| **RETURNS** | <tt>Tuple[Floats3d, Tuple[Floats2d, Floats1d, Floats1d, Floats1d]]</tt> | The gradients for the inputs and parameters (the weights, biases, initial hiddens and initial cells). |
### Ops.maxout {#maxout tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
| Argument | Type | Description |
| ----------- | -------------------------------- | ------------------------------------------------------------------------------- |
| `X` | <tt>Floats3d</tt> | The inputs. |
| **RETURNS** | <tt>Tuple[Floats2d, Ints2d]</tt> | The outputs and an array indicating which elements in the final axis were used. |
### Ops.backprop_maxout {#backprop_maxout tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------- |
| `dY` | <tt>Floats2d</tt> | Gradients of the output array. |
| `which` | <tt>Ints2d</tt> | The positions selected in the forward pass. |
| `P` | <tt>int</tt> | The size of the final dimension. |
| **RETURNS** | <tt>Floats3d</tt> | The gradient of the inputs. |
### Ops.relu {#relu tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
| Argument | Type | Description |
| -------------- | ----------------- | ------------------------------------------ |
| `X` | <tt>Floats2d</tt> | The inputs. |
| _keyword-only_ | | |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>Floats2d</tt> | The outputs. |
### Ops.backprop_relu {#relu tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
| Argument | Type | Description |
| -------------- | ----------------- | ------------------------------------------ |
| `dY` | <tt>Floats2d</tt> | Gradients of the output array. |
| `Y` | <tt>Floats2d</tt> | The output from the forward pass. |
| _keyword-only_ | | |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>Floats2d</tt> | The gradient of the input. |
### Ops.mish {#mish tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Compute the Mish activation
([Misra, 2019](https://arxiv.org/pdf/1908.08681.pdf)).
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `threshold` | <tt>float</tt> | Maximum value at which to apply the activation. |
| `inplace` | <tt>bool</tt> | Apply Mish to `X` in-place. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_mish {#backprop_mish tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the Mish activation
([Misra, 2019](https://arxiv.org/pdf/1908.08681.pdf)).
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `threshold` | <tt>float</tt> | Threshold from the forward pass. |
| `inplace` | <tt>bool</tt> | Apply Mish backprop to `dY` in-place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.swish {#swish tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Swish [(Ramachandran et al., 2017)](https://arxiv.org/abs/1710.05941v2) is a
self-gating non-monotonic activation function similar to the [GELU](#gelu)
activation: whereas [GELU](#gelu) uses the CDF of the Gaussian distribution Φ
for self-gating `x * Φ(x)`, Swish uses the logistic CDF `x * σ(x)`. Sometimes
referred to as "SiLU" for "Sigmoid Linear Unit".
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_swish {#backprop_swish tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the Swish activation
[(Ramachandran et al., 2017)](https://arxiv.org/abs/1710.05941v2).
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `Y` | <tt>FloatsXd</tt> | The outputs to the forward pass. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.dish {#dish tag="method" new="8.1.1"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Dish or "Daniël's Swish-like activation" is an activation function with a
non-monotinic shape similar to [GELU](#gelu), [Swish](#swish) and [Mish](#mish).
However, Dish does not rely on elementary functions like `exp` or `erf`, making
it much
[faster to compute](https://twitter.com/danieldekok/status/1484898130441166853)
in most cases.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_dish {#backprop_dish tag="method" new="8.1.1"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the Dish activation.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.gelu {#gelu tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
GELU or "Gaussian Error Linear Unit"
[(Hendrycks and Gimpel, 2016)](https://arxiv.org/abs/1606.08415) is a
self-gating non-monotonic activation function similar to the [Swish](#swish)
activation: whereas [GELU](#gelu) uses the CDF of the Gaussian distribution Φ
for self-gating `x * Φ(x)` the Swish activation uses the logistic CDF σ and
computes `x * σ(x)`. Various approximations exist, but `thinc` implements the
exact GELU. The use of GELU is popular within transformer feed-forward blocks.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_gelu {#backprop_gelu tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the GELU activation
[(Hendrycks and Gimpel, 2016)](https://arxiv.org/abs/1606.08415).
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.relu_k {#relu_k tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
ReLU activation function with the maximum value clipped at `k`. A common choice
is `k=6` introduced for convolutional deep belief networks
[(Krizhevsky, 2010)](https://www.cs.toronto.edu/~kriz/conv-cifar10-aug2010.pdf).
The resulting function `relu6` is commonly used in low-precision scenarios.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| `k` | <tt>float</tt> | Maximum value (default: 6.0). |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_relu_k {#backprop_relu_k tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the ReLU-k activation.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.hard_sigmoid {#hard_sigmoid tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
The hard sigmoid activation function is a fast linear approximation of the
sigmoid activation, defined as `max(0, min(1, x * 0.2 + 0.5))`.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_hard_sigmoid {#backprop_hard_sigmoid tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the hard sigmoid activation.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.hard_tanh {#hard_tanh tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
The hard tanh activation function is a fast linear approximation of tanh,
defined as `max(-1, min(1, x))`.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_hard_tanh {#backprop_hard_tanh tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the hard tanh activation.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.clipped_linear {#clipped_linear tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Flexible clipped linear activation function of the form
`max(min_value, min(max_value, x * slope + offset))`. It is used to implement
the [`relu_k`](#reluk), [`hard_sigmoid`](#hard_sigmoid), and
[`hard_tanh`](#hard_tanh) methods.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------------------------------------- |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| `slope` | <tt>float</tt> | The slope of the linear function: `input * slope`. |
| `offset` | <tt>float</tt> | The offset or intercept of the linear function: `input * slope + offset`. |
| `min_val` | <tt>float</tt> | Minimum value to clip to. |
| `max_val` | <tt>float</tt> | Maximum value to clip to. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_clipped_linear {#backprop_clipped_linear tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the clipped linear activation.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `slope` | <tt>float</tt> | The slope of the linear function: `input * slope`. |
| `offset` | <tt>float</tt> | The offset or intercept of the linear function: `input * slope + offset`. |
| `min_val` | <tt>float</tt> | Minimum value to clip to. |
| `max_val` | <tt>float</tt> | Maximum value to clip to. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.hard_swish {#hard_swish tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
The hard Swish activation function is a fast linear approximation of Swish:
`x * hard_sigmoid(x)`.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_hard_swish {#backprop_hard_swish tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the hard Swish activation.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.hard_swish_mobilenet {#hard_swish_mobilenet tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
A variant of the fast hard Swish activation function used in `MobileNetV3`
[(Howard et al., 2019)](https://arxiv.org/abs/1905.02244), defined as
`x * (relu6(x + 3) / 6)`.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------ |
| `X` | <tt>FloatsXd</tt> | The inputs. |
| `inplace` | <tt>bool</tt> | If `True`, the array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The outputs. |
### Ops.backprop_hard_swish_mobilenet {#backprop_hard_swish_mobilenet tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="no"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the hard Swish MobileNet activation.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------------------------- |
| `dY` | <tt>FloatsXd</tt> | Gradients of the output array. |
| `X` | <tt>FloatsXd</tt> | The inputs to the forward pass. |
| `inplace` | <tt>bool</tt> | If `True`, the `dY` array is modified in place. |
| **RETURNS** | <tt>FloatsXd</tt> | The gradient of the input. |
### Ops.reduce_first {#reduce_first tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Perform sequence-wise first pooling for data in the ragged format.
- Zero-length sequences are not allowed. A `ValueError` is raised if any element
in `lengths` is zero.
- Batch and hidden dimensions can have a size of zero. In these cases the
corresponding dimensions in the output also have a size of zero.
| Argument | Type | Description |
| ----------- | ------------------------------- | --------------------------------------------------------------------- |
| `X` | <tt>Floats2d</tt> | The concatenated sequences. |
| `lengths` | <tt>Ints1d</tt> | The sequence lengths. |
| **RETURNS** | <tt>Tuple[Floats2d,Ints1d]</tt> | The first vector of each sequence and the sequence start/end indices. |
### Ops.backprop_reduce_first {#backprop_reduce_first tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Backpropagate the `reduce_first` operation.
| Argument | Type | Description |
| ------------- | ----------------- | ------------------------------------------- |
| `d_firsts` | <tt>Floats2d</tt> | The gradient of the outputs. |
| `starts_ends` | <tt>Ints1d</tt> | The sequence start/end indices. |
| **RETURNS** | <tt>Floats2d</tt> | The gradient of the concatenated sequences. |
### Ops.reduce_last {#reduce_last tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Perform sequence-wise last pooling for data in the ragged format.
- Zero-length sequences are not allowed. A `ValueError` is raised if any element
in `lengths` is zero.
- Batch and hidden dimensions can have a size of zero. In these cases the
corresponding dimensions in the output also have a size of zero.
| Argument | Type | Description |
| ----------- | ------------------------------- | ------------------------------------------------------------------------------- |
| `X` | <tt>Floats2d</tt> | The concatenated sequences. |
| `lengths` | <tt>Ints1d</tt> | The sequence lengths. |
| **RETURNS** | <tt>Tuple[Floats2d,Ints1d]</tt> | The last vector of each sequence and the indices of the last sequence elements. |
### Ops.backprop_reduce_last {#backprop_reduce_last tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** default
- **cupy:** default
</inline-list>
Backpropagate the `reduce_last` operation.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------- |
| `d_lasts` | <tt>Floats2d</tt> | The gradient of the outputs. |
| `lasts` | <tt>Ints1d</tt> | Indices of the last sequence elements. |
| **RETURNS** | <tt>Floats2d</tt> | The gradient of the concatenated sequences. |
### Ops.reduce_sum {#reduce_sum tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Perform sequence-wise summation for data in the ragged format.
- Zero-length sequences are reduced to all-zero vectors.
- Batch and hidden dimensions can have a size of zero. In these cases the
corresponding dimensions in the output also have a size of zero.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------------- |
| `X` | <tt>Floats2d</tt> | The concatenated sequences. |
| `lengths` | <tt>Ints1d</tt> | The sequence lengths. |
| **RETURNS** | <tt>Floats2d</tt> | The sequence-wise summations. |
### Ops.backprop_reduce_sum {#backprop_reduce_sum tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the `reduce_sum` operation.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------- |
| `d_sums` | <tt>Floats2d</tt> | The gradient of the outputs. |
| `lengths` | <tt>Ints1d</tt> | The sequence lengths. |
| **RETURNS** | <tt>Floats2d</tt> | The gradient of the concatenated sequences. |
### Ops.reduce_mean {#reduce_mean tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Perform sequence-wise averaging for data in the ragged format.
- Zero-length sequences are reduced to all-zero vectors.
- Batch and hidden dimensions can have a size of zero. In these cases the
corresponding dimensions in the output also have a size of zero.
| Argument | Type | Description |
| ----------- | ----------------- | --------------------------- |
| `X` | <tt>Floats2d</tt> | The concatenated sequences. |
| `lengths` | <tt>Ints1d</tt> | The sequence lengths. |
| **RETURNS** | <tt>Floats2d</tt> | The sequence-wise averages. |
### Ops.backprop_reduce_mean {#backprop_reduce_mean tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the `reduce_mean` operation.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------- |
| `d_means` | <tt>Floats2d</tt> | The gradient of the outputs. |
| `lengths` | <tt>Ints1d</tt> | The sequence lengths. |
| **RETURNS** | <tt>Floats2d</tt> | The gradient of the concatenated sequences. |
### Ops.reduce_max {#reduce_max tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Perform sequence-wise max pooling for data in the ragged format. Zero-length
sequences are not allowed.
- Zero-length sequences are not allowed. A `ValueError` is raised if any element
in `lengths` is zero.
- Batch and hidden dimensions can have a size of zero. In these cases the
corresponding dimensions in the output also have a size of zero.
| Argument | Type | Description |
| ----------- | -------------------------------- | --------------------------- |
| `X` | <tt>Floats2d</tt> | The concatenated sequences. |
| `lengths` | <tt>Ints1d</tt> | The sequence lengths. |
| **RETURNS** | <tt>Tuple[Floats2d, Ints2d]</tt> | The sequence-wise maximums. |
### Ops.backprop_reduce_max {#backprop_reduce_max tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Backpropagate the `reduce_max` operation.
| Argument | Type | Description |
| ----------- | ----------------- | ------------------------------------------- |
| `d_maxes` | <tt>Floats2d</tt> | The gradient of the outputs. |
| `which` | <tt>Ints2d</tt> | The indices selected. |
| `lengths` | <tt>Ints1d</tt> | The sequence lengths. |
| **RETURNS** | <tt>Floats2d</tt> | The gradient of the concatenated sequences. |
### Ops.hash {#hash tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Hash a sequence of 64-bit keys into a table with four 32-bit keys, using
`murmurhash3`.
| Argument | Type | Description |
| ----------- | --------------- | ----------------------------------- |
| `ids` | <tt>Ints1d</tt> | The keys, 64-bit unsigned integers. |
| `seed` | <tt>int</tt> | The hashing seed. |
| **RETURNS** | <tt>Ints2d</tt> | The hashes. |
### Ops.ngrams {#ngrams tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** default
</inline-list>
Create hashed ngram features.
| Argument | Type | Description |
| ----------- | --------------- | ------------------------------------------ |
| `n` | <tt>int</tt> | The window to calculate each feature over. |
| `keys` | <tt>Ints1d</tt> | The input sequence. |
| **RETURNS** | <tt>Ints1d</tt> | The hashed ngrams. |
### Ops.gather_add {#gather_add tag="method" new="8.1"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Gather rows from `table` with shape `(T, O)` using array `indices` with shape
`(B, K)`, then sum the resulting array with shape `(B, K, O)` over the `K` axis.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------- |
| `table` | <tt>Floats2d</tt> | The array to increment. |
| `indices` | <tt>Ints2d</tt> | The indices to use. |
| **RETURNS** | <tt>Floats2d</tt> | The summed rows. |
### Ops.scatter_add {#scatter_add tag="method"}
<inline-list>
- **default:** <i name="yes"></i>
- **numpy:** <i name="yes"></i>
- **cupy:** <i name="yes"></i>
</inline-list>
Increment entries in the array out using the indices in `ids` and the values in
`inputs`.
| Argument | Type | Description |
| ----------- | ----------------- | ----------------------- |
| `table` | <tt>FloatsXd</tt> | The array to increment. |
| `indices` | <tt>IntsXd</tt> | The indices to use. |
| `values` | <tt>FloatsXd</tt> | The inputs. |
| **RETURNS** | <tt>FloatsXd</tt> | The incremented array. |
---
## Utilities {#util}
### get_ops {#get_ops tag="function"}
Get a backend object using a string name.
```python
### Example
from thinc.api import get_ops
numpy_ops = get_ops("numpy")
```
| Argument | Type | Description |
| ----------- | ------------ | ----------------------------------------------------- |
| `ops` | <tt>str</tt> | `"numpy"`, `"apple"`, `"cupy"` or `"mps"`. |
| `**kwargs` | | Optional arguments passed to [`Ops.__init__`](#init). |
| **RETURNS** | <tt>Ops</tt> | The backend object. |
### use_ops {#use_ops tag="contextmanager"}
Change the backend to execute with for the scope of the block.
```python
### Example
from thinc.api import use_ops, get_current_ops
with use_ops("cupy"):
current_ops = get_current_ops()
assert current_ops.name == "cupy"
```
| Argument | Type | Description |
| ---------- | ------------ | ----------------------------------------------------- |
| `ops` | <tt>str</tt> | `"numpy"`, `"apple"`, `"cupy"` or `"mps"`. |
| `**kwargs` | | Optional arguments passed to [`Ops.__init__`](#init). |
### get_current_ops {#get_current_ops tag="function"}
Get the current backend object.
| Argument | Type | Description |
| ----------- | ------------ | --------------------------- |
| **RETURNS** | <tt>Ops</tt> | The current backend object. |
### set_current_ops {#set_current_ops tag="function"}
Set the current backend object.
| Argument | Type | Description |
| -------- | ------------ | ------------------- |
| `ops` | <tt>Ops</tt> | The backend object. |
### set_gpu_allocator {#set_gpu_allocator tag="function"}
Set the CuPy GPU memory allocator.
| Argument | Type | Description |
| ----------- | ------------ | ------------------------------------- |
| `allocator` | <tt>str</tt> | Either `"pytorch"` or `"tensorflow"`. |
```python
### Example
from thinc.api set_gpu_allocator
set_gpu_allocator("pytorch")
```
|