File size: 80,279 Bytes
9375c9a |
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 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 |
// Copyright (C) 2015 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_DNn_CORE_ABSTRACT_H_
#ifdef DLIB_DNn_CORE_ABSTRACT_H_
#include "../cuda/tensor_abstract.h"
#include <memory>
#include <type_traits>
#include <tuple>
#include <vector>
#include "../rand.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
template <
typename... T
>
auto tuple_tail(
const std::tuple<T...>& item
);
/*!
ensures
- returns a tuple that contains everything in item except for tuple_head(item).
The items will be in the same order as they are in item, just without
tuple_head(item).
- This function will correctly handle nested tuples.
!*/
template <typename... T>
auto tuple_head (
const std::tuple<T...>& item
);
/*!
ensures
- returns a copy of the first thing in the tuple that isn't a std::tuple.
Essentially, this function calls std::get<0>() recursively on item until
a non-std::tuple object is found.
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
double get_learning_rate_multiplier(
const T& obj
);
/*!
ensures
- if (obj has a get_learning_rate_multiplier() member function) then
- returns obj.get_learning_rate_multiplier()
- else
- returns 1
!*/
template <typename T>
void set_learning_rate_multiplier(
T& obj,
double learning_rate_multiplier
);
/*!
requires
- learning_rate_multiplier >= 0
ensures
- if (obj has a set_learning_rate_multiplier() member function) then
- calls obj.set_learning_rate_multiplier(learning_rate_multiplier)
- else
- does nothing
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
double get_bias_learning_rate_multiplier(
const T& obj
);
/*!
ensures
- if (obj has a get_bias_learning_rate_multiplier() member function) then
- returns obj.get_bias_learning_rate_multiplier()
- else
- returns 1
!*/
template <typename T>
void set_bias_learning_rate_multiplier(
T& obj,
double bias_learning_rate_multiplier
);
/*!
requires
- bias_learning_rate_multiplier >= 0
ensures
- if (obj has a set_bias_learning_rate_multiplier() member function) then
- calls obj.set_bias_learning_rate_multiplier(bias_learning_rate_multiplier)
- else
- does nothing
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
double get_weight_decay_multiplier(
const T& obj
);
/*!
ensures
- if (obj has a get_weight_decay_multiplier() member function) then
- returns obj.get_weight_decay_multiplier()
- else
- returns 1
!*/
template <typename T>
void set_weight_decay_multiplier(
T& obj,
double weight_decay_multiplier
);
/*!
requires
- weight_decay_multiplier >= 0
ensures
- if (obj has a set_weight_decay_multiplier() member function) then
- calls obj.set_weight_decay_multiplier(weight_decay_multiplier)
- else
- does nothing
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
double get_bias_weight_decay_multiplier(
const T& obj
);
/*!
ensures
- if (obj has a get_bias_weight_decay_multiplier() member function) then
- returns obj.get_bias_weight_decay_multiplier()
- else
- returns 1
!*/
template <typename T>
void set_bias_weight_decay_multiplier(
T& obj,
double bias_weight_decay_multiplier
);
/*!
requires:
- bias_weight_decay_multiplier >= 0
ensures
- if (obj has a set_bias_weight_decay_multiplier() member function) then
- calls obj.set_bias_weight_decay_multiplier(bias_weight_decay_multiplier)
- else
- does nothing
!*/
// ----------------------------------------------------------------------------------------
template <typename T>
void disable_bias(
T& obj
);
/*!
ensures
- if (obj has a disable_bias() member function) then
- calls obj.disable_bias()
- else
- does nothing
!*/
// ----------------------------------------------------------------------------------------
bool dnn_prefer_fastest_algorithms(
);
/*!
ensures
- If dlib should prefer to use fast algorithms rather than ones that use less
RAM then this function returns true and false otherwise.
- On program startup this function will default to true.
!*/
void set_dnn_prefer_fastest_algorithms(
);
/*!
ensures
- #dnn_prefer_fastest_algorithms() == true
!*/
void set_dnn_prefer_smallest_algorithms(
);
/*!
ensures
- #dnn_prefer_fastest_algorithms() == false
!*/
// ----------------------------------------------------------------------------------------
template <
typename T
>
class sstack
{
/*!
WHAT THIS OBJECT REPRESENTS
This is a basic stack of T objects. It contains no data itself but simply
points to a memory range of T object and allows you to access that block of
T objects as a stack.
!*/
public:
typedef T value_type;
sstack() = delete;
sstack (
T* data,
size_t s
);
/*!
ensures
- #size() == s
- #top() == *data
- #pop(i).top() == data[i]
!*/
const T& top(
) const;
/*!
requires
- size() != 0
ensures
- returns the top element of the stack.
!*/
T& top(
);
/*!
requires
- size() != 0
ensures
- returns the top element of the stack.
!*/
size_t size(
) const;
/*!
ensures
- returns the number of elements in this stack.
!*/
sstack pop(
size_t num = 1
);
/*!
requires
- num <= size()
ensures
- returns a reference to the sub-stack S such that:
- S.size() == size()-num.
- S.top() is num elements down the stack.
!*/
};
template <
typename T
>
sstack<T> make_sstack(
std::vector<T>& item
) { return sstack<T>(item.data(), item.size()); }
/*!
ensures
- returns a sstack that sits on top of the given std::vector.
!*/
// ----------------------------------------------------------------------------------------
template <
typename LAYER_DETAILS,
typename SUBNET
>
class add_layer
{
/*!
REQUIREMENTS ON LAYER_DETAILS
- Must be a type that implements the EXAMPLE_COMPUTATIONAL_LAYER_ interface
defined in layers_abstract.h
REQUIREMENTS ON SUBNET
- One of the following must be true:
- SUBNET implements the EXAMPLE_INPUT_LAYER interface defined in
input_abstract.h.
- SUBNET is an add_layer object.
- SUBNET is an add_tag_layer object.
- SUBNET is an add_skip_layer object.
- SUBNET is a repeat object.
WHAT THIS OBJECT REPRESENTS
This object represents a deep neural network. In particular, it is a tool
for adding another layer on top of the neural network of type SUBNET, which
is specified as a template argument. The specific layer added is defined
by the LAYER_DETAILS details template argument.
!*/
public:
typedef LAYER_DETAILS layer_details_type;
typedef SUBNET subnet_type;
typedef typename subnet_type::input_type input_type;
// num_computational_layers will always give the number of layers in the network
// that transform tensors (i.e. layers defined by something that implements the
// EXAMPLE_COMPUTATIONAL_LAYER_ interface). This is all the layers except for
// loss, tag, and skip layers.
const static size_t num_computational_layers = subnet_type::num_computational_layers + 1;
// num_layers counts all the layers in the network regardless of their type.
const static size_t num_layers = subnet_type::num_layers + 1;
add_layer(
);
/*!
ensures
- default constructs all the layers in this network.
- #sample_expansion_factor() == 0
!*/
add_layer(const add_layer&) = default;
add_layer(add_layer&&) = default;
add_layer& operator=(add_layer&&) = default;
add_layer& operator=(const add_layer&) = default;
/*!
ensures
- this object is copyable and movable.
!*/
template <typename T, typename U>
add_layer(
const add_layer<T,U>& item
);
/*!
ensures
- This constructor allows you to copy neural network objects from one to
another as long as their corresponding layers can be constructed from
each other.
- #layer_details() == layer_details_type(item.layer_details())
- #subnet() == subnet_type(item.subnet())
- #sample_expansion_factor() == item.sample_expansion_factor()
!*/
template <typename ...T, typename LD, typename ...U>
add_layer(
const std::tuple<LD,U...>& layer_det,
T&& ...args
);
/*!
ensures
- #layer_details() == layer_details_type(tuple_head(layer_det))
- #subnet() == subnet_type(tuple_tail(layer_det),args)
- #sample_expansion_factor() == 0
!*/
template <typename ...T>
add_layer(
const layer_details_type& layer_det,
T&& ...args
);
/*!
ensures
- #layer_details() == layer_details_type(layer_det)
- #subnet() == subnet_type(args)
- #sample_expansion_factor() == 0
!*/
template <typename ...T>
add_layer(
T&& ...args
);
/*!
ensures
- This version of the constructor is only called if layer_details_type
can't be constructed from the first thing in args. In this case, the
args are simply passed on to the sub layers in their entirety.
- #layer_details() == layer_details_type()
- #subnet() == subnet_type(args)
- #sample_expansion_factor() == 0
!*/
template <typename ...T>
add_layer(
layer_details_type&& layer_det,
T&& ...args
);
/*!
ensures
- #layer_details() == layer_det
- #subnet() == subnet_type(args)
- #sample_expansion_factor() == 0
!*/
template <typename forward_iterator>
void to_tensor (
forward_iterator ibegin,
forward_iterator iend,
resizable_tensor& data
) const;
/*!
requires
- [ibegin, iend) is an iterator range over input_type objects.
- std::distance(ibegin,iend) > 0
ensures
- Converts the iterator range into a tensor and stores it into #data.
- #data.num_samples()%distance(ibegin,iend) == 0.
- #sample_expansion_factor() == #data.num_samples()/distance(ibegin,iend).
- #sample_expansion_factor() > 0
- The data in the ith sample of #data corresponds to the input_type object
*(ibegin+i/#sample_expansion_factor()).
- Invokes data.async_copy_to_device() so that the data begins transferring
to the GPU device, if present.
- This function is implemented by calling the to_tensor() routine defined
at the input layer of this network.
!*/
unsigned int sample_expansion_factor (
) const;
/*!
ensures
- When to_tensor() is invoked on this network's input layer it converts N
input objects into M samples, all stored inside a resizable_tensor. It
is always the case that M is some integer multiple of N.
sample_expansion_factor() returns the value of this multiplier. To be
very specific, it is always true that M==I*N where I is some integer.
This integer I is what is returned by sample_expansion_factor().
!*/
const subnet_type& subnet(
) const;
/*!
ensures
- returns the immediate subnetwork of *this network.
!*/
subnet_type& subnet(
);
/*!
ensures
- returns the immediate subnetwork of *this network.
!*/
const layer_details_type& layer_details(
) const;
/*!
ensures
- returns the layer_details_type instance that defines the behavior of the
layer at the top of this network. I.e. returns the layer details that
defines the behavior of the layer nearest to the network output rather
than the input layer.
!*/
layer_details_type& layer_details(
);
/*!
ensures
- returns the layer_details_type instance that defines the behavior of the
layer at the top of this network. I.e. returns the layer details that
defines the behavior of the layer nearest to the network output rather
than the input layer.
!*/
template <typename forward_iterator>
const tensor& operator() (
forward_iterator ibegin,
forward_iterator iend
);
/*!
requires
- [ibegin, iend) is an iterator range over input_type objects.
- std::distance(ibegin,iend) > 0
ensures
- runs [ibegin,iend) through the network and returns the results.
In particular, this function performs:
to_tensor(ibegin,iend,temp_tensor);
return forward(temp_tensor);
- The return value from this function is also available in #get_output().
i.e. this function returns #get_output().
- have_same_dimensions(#get_gradient_input(), #get_output()) == true.
- All elements of #get_gradient_input() are set to 0.
i.e. calling this function clears out #get_gradient_input() and ensures
it has the same dimensions as the most recent output.
!*/
const tensor& operator() (
const input_type& x
);
/*!
ensures
- runs a single x through the network and returns the output.
I.e. returns (*this)(&x, &x+1);
!*/
const tensor& forward(
const tensor& x
);
/*!
requires
- sample_expansion_factor() != 0
(i.e. to_tensor() must have been called to set sample_expansion_factor()
to something non-zero.)
- x.num_samples()%sample_expansion_factor() == 0
- x.num_samples() > 0
ensures
- Runs x through the network and returns the results. In particular, this
function performs the equivalent of:
subnet().forward(x);
if (this is the first time forward() has been called) then
layer_details().setup(subnet());
layer_details().forward(subnet(), get_output());
- The return value from this function is also available in #get_output().
i.e. this function returns #get_output().
- have_same_dimensions(#get_gradient_input(), #get_output()) == true
- All elements of #get_gradient_input() are set to 0.
i.e. calling this function clears out #get_gradient_input() and ensures
it has the same dimensions as the most recent output.
!*/
const tensor& get_output(
) const;
/*!
ensures
- returns the output for the last tensor that was run through the network.
If nothing has been run through the network yet then returns an empty
tensor.
!*/
tensor& get_gradient_input(
);
/*!
ensures
- returns the error gradient for this network. That is, this is the error
gradient that this network will use to compute parameter gradients when
back_propagate_error() is called. Therefore, when performing back
propagation, layers that sit on top of this network layer write their
back-propagated error gradients into get_gradient_input(). Or to put it
another way, during back-propagation, layers take the contents of their
get_gradient_input() and back-propagate it through themselves and store
the result into their subnetwork's get_gradient_input().
This means you should consider get_gradient_input() as an input to the
back_propagate_error() method.
!*/
const tensor& get_final_data_gradient(
) const;
/*!
ensures
- if back_propagate_error() has been called to back-propagate a gradient
through this network then you can call get_final_data_gradient() to
obtain the last data gradient computed. That is, this function returns
the gradient of the network with respect to its inputs.
- Note that there is only one "final data gradient" for an entire network,
not one per layer, since there is only one input to the entire network.
!*/
const tensor& get_parameter_gradient(
) const;
/*!
ensures
- if back_propagate_error() has been called then you can call
get_parameter_gradient() to find the gradient of this layer's parameters.
When we update the parameters by calling update_parameters(), it will use
the gradient in get_parameter_gradient() to perform the update.
Therefore, you should consider get_parameter_gradient() as an input to
update_parameters().
!*/
tensor& get_parameter_gradient (
);
/*!
ensures
- returns a non-const reference to the tensor returned by the above
get_parameter_gradient() method. You could use this method to modify the
parameter gradient in some way before invoking update_parameters().
!*/
void back_propagate_error(
const tensor& x
);
/*!
requires
- forward(x) was called to forward propagate x though the network.
Moreover, this was the most recent call to forward() and x has not been
subsequently modified in any way.
- get_gradient_input() has been set equal to the gradient of this network's
output with respect to some loss function.
ensures
- Back propagates the error gradient, get_gradient_input(), through this
network and computes parameter and data gradients, via backpropagation.
Specifically, this function populates get_final_data_gradient() and also,
for each layer, the tensor returned by get_parameter_gradient().
- All elements of #get_gradient_input() are set to 0.
- have_same_dimensions(#get_final_data_gradient(), x) == true.
- have_same_dimensions(#get_parameter_gradient(), layer_details().get_layer_params()) == true.
- #get_final_data_gradient() contains the gradient of the network with
respect to x.
!*/
void back_propagate_error(
const tensor& x,
const tensor& gradient_input
);
/*!
requires
- forward(x) was called to forward propagate x though the network.
Moreover, this was the most recent call to forward() and x has not been
subsequently modified in any way.
- have_same_dimensions(gradient_input, get_output()) == true
ensures
- This function is identical to the version of back_propagate_error()
defined immediately above except that it back-propagates gradient_input
through the network instead of get_gradient_input(). Therefore, this
version of back_propagate_error() is equivalent to performing:
get_gradient_input() = gradient_input;
back_propagate_error(x);
Except that calling back_propagate_error(x,gradient_input) avoids the
copy and is therefore slightly more efficient.
- All elements of #get_gradient_input() are set to 0.
- have_same_dimensions(#get_final_data_gradient(), x) == true.
- have_same_dimensions(#get_parameter_gradient(), layer_details().get_layer_params()) == true.
- #get_final_data_gradient() contains the gradient of the network with
respect to x.
!*/
template <typename solver_type>
void update_parameters(
sstack<solver_type> solvers,
double learning_rate
);
/*!
requires
- solver_type is an implementation of the EXAMPLE_SOLVER interface defined
in solvers_abstract.h
- back_propagate_error() has been called.
- The given solvers have only ever been used with this network. That is,
if you want to call update_parameters() on some other neural network
object then you must NOT reuse the same solvers object.
- solvers.size() >= num_computational_layers
- 0 < learning_rate <= 1
ensures
- Updates all the parameters in the network. In particular, we pass each
layer's parameter gradient (i.e. the tensor returned by the layer's
get_parameter_gradient() member) through that layer's corresponding
solver object. This produces a parameter delta vector which we add to
the layer's parameters.
- The solvers use the given learning rate.
!*/
template <typename solver_type>
void update_parameters(std::vector<solver_type>& solvers, double learning_rate)
{ update_parameters(make_sstack(solvers), learning_rate); }
/*!
Convenience method for calling update_parameters()
!*/
void clean(
);
/*!
ensures
- Causes the network to forget about everything but its parameters.
That is, for each layer we will have:
- get_output().num_samples() == 0
- get_gradient_input().num_samples() == 0
However, running new input data though this network will still produce
the same output it would have produced regardless of any calls to
clean(). The purpose of clean() is to compact the network object prior
to saving it to disk so that it takes up less space and the IO is
quicker.
- This also calls the .clean() method on any layer details objects that
define a .clean() method.
!*/
};
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const add_layer<T,U>& item);
/*!
prints the network architecture to the given output stream.
!*/
template <typename T, typename U>
void serialize(const add_layer<T,U>& item, std::ostream& out);
template <typename T, typename U>
void deserialize(add_layer<T,U>& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class no_label_type;
template <
typename LOSS_DETAILS,
typename SUBNET
>
class add_loss_layer
{
/*!
REQUIREMENTS ON LOSS_DETAILS
- Must be a type that implements the EXAMPLE_LOSS_LAYER_ interface defined
in loss_abstract.h
REQUIREMENTS ON SUBNET
- One of the following must be true:
- SUBNET is an add_layer object.
- SUBNET is an add_tag_layer object.
- SUBNET is an add_skip_layer object.
- SUBNET is a repeat object.
WHAT THIS OBJECT REPRESENTS
This object represents a deep neural network. In particular, it is a tool
for adding a loss layer on top of the neural network of type SUBNET, which
is specified as a template argument. The specific layer added is defined
by the LOSS_DETAILS details template argument. Importantly, a loss layer
is the last layer in a deep neural network. So once it is added you can't
add any other layers of any type.
!*/
public:
typedef LOSS_DETAILS loss_details_type;
typedef SUBNET subnet_type;
typedef typename subnet_type::input_type input_type;
const static size_t num_computational_layers = subnet_type::num_computational_layers;
const static size_t num_layers = subnet_type::num_layers + 1;
// If LOSS_DETAILS is an unsupervised loss then training_label_type==no_label_type.
// Otherwise it is defined as follows:
typedef typename LOSS_DETAILS::training_label_type training_label_type;
// Similarly, if LOSS_DETAILS doesn't provide any output conversion then
// output_label_type==no_label_type.
typedef typename LOSS_DETAILS::output_label_type output_label_type;
add_loss_layer() = default;
/*!
ensures
- default constructs all the layers in this network.
!*/
add_loss_layer(const add_loss_layer&) = default;
add_loss_layer(add_loss_layer&&) = default;
add_loss_layer& operator=(add_loss_layer&&) = default;
add_loss_layer& operator=(const add_loss_layer&) = default;
/*!
ensures
- this object is copyable and movable.
!*/
template <typename T, typename U>
add_loss_layer(
const add_loss_layer<T,U>& item
);
/*!
ensures
- This constructor allows you to copy neural network objects from one to
another as long as their corresponding layers can be constructed from
each other.
- #loss_details() == loss_details_type(item.loss_details())
- #subnet() == subnet_type(item.subnet())
!*/
template <typename ...T>
add_loss_layer(
const LOSS_DETAILS& layer_det,
T&& ...args
);
/*!
ensures
- #loss_details() == loss_details_type(layer_det)
- #subnet() == subnet_type(args)
!*/
template <typename ...T>
add_loss_layer(
LOSS_DETAILS&& layer_det,
T&& ...args
);
/*!
ensures
- #loss_details() == loss_details_type(layer_det)
- #subnet() == subnet_type(args)
!*/
template <typename ...T>
add_loss_layer(
T&& ...args
);
/*!
ensures
- This version of the constructor is only called if loss_details_type can't
be constructed from the first thing in args. In this case, the args are
simply passed on to the sub layers in their entirety.
- #loss_details() == loss_details_type()
- #subnet() == subnet_type(args)
!*/
const subnet_type& subnet(
) const;
/*!
ensures
- returns the immediate subnetwork of *this network.
!*/
subnet_type& subnet(
);
/*!
ensures
- returns the immediate subnetwork of *this network.
!*/
const loss_details_type& loss_details(
) const;
/*!
ensures
- returns the loss_details_type instance that defines the behavior of the
loss layer used by this network.
!*/
loss_details_type& loss_details(
);
/*!
ensures
- returns the loss_details_type instance that defines the behavior of the
loss layer used by this network.
!*/
template <typename forward_iterator>
void to_tensor (
forward_iterator ibegin,
forward_iterator iend,
resizable_tensor& data
) const;
/*!
requires
- [ibegin, iend) is an iterator range over input_type objects.
- std::distance(ibegin,iend) > 0
ensures
- Converts the iterator range into a tensor and stores it into #data.
- #data.num_samples()%distance(ibegin,iend) == 0.
- #sample_expansion_factor() == #data.num_samples()/distance(ibegin,iend).
- #sample_expansion_factor() > 0
- The data in the ith sample of #data corresponds to the input_type object
*(ibegin+i/sample_expansion_factor()).
- Invokes data.async_copy_to_device() so that the data begins transferring
to the GPU device, if present.
- This function is implemented by calling the to_tensor() routine defined
at the input layer of this network.
!*/
unsigned int sample_expansion_factor (
) const;
/*!
ensures
- When to_tensor() is invoked on this network's input layer it converts N
input objects into M samples, all stored inside a resizable_tensor. It
is always the case that M is some integer multiple of N.
sample_expansion_factor() returns the value of this multiplier. To be
very specific, it is always true that M==I*N where I is some integer.
This integer I is what is returned by sample_expansion_factor().
!*/
// -------------
const tensor& forward(const tensor& x
);
/*!
requires
- sample_expansion_factor() != 0
(i.e. to_tensor() must have been called to set sample_expansion_factor()
to something non-zero.)
- x.num_samples()%sample_expansion_factor() == 0
- x.num_samples() > 0
ensures
- Runs x through the network and returns the results as a tensor. In particular,
this function just performs:
return subnet().forward(x);
So if you want to get the outputs as an output_label_type then call one of the
methods below instead, like operator().
- The return value from this function is also available in #subnet().get_output().
i.e. this function returns #subnet().get_output().
- have_same_dimensions(#subnet().get_gradient_input(), #subnet().get_output()) == true
- All elements of #subnet().get_gradient_input() are set to 0.
i.e. calling this function clears out #subnet().get_gradient_input() and ensures
it has the same dimensions as the most recent output.
!*/
template <typename output_iterator>
void operator() (
const tensor& x,
output_iterator obegin
);
/*!
requires
- sample_expansion_factor() != 0
(i.e. to_tensor() must have been called to set sample_expansion_factor()
to something non-zero.)
- x.num_samples()%sample_expansion_factor() == 0
- x.num_samples() > 0
- obegin == iterator pointing to the start of a range of
x.num_samples()/sample_expansion_factor() output_label_type elements.
ensures
- runs x through the network and writes the output to the range at obegin.
- loss_details().to_label() is used to write the network output into
obegin.
!*/
template <typename forward_iterator, typename label_iterator>
void operator() (
forward_iterator ibegin,
forward_iterator iend,
label_iterator obegin
);
/*!
requires
- [ibegin, iend) is an iterator range over input_type objects.
- std::distance(ibegin,iend) > 0
- obegin == iterator pointing to the start of a range of
std::distance(ibegin,iend) output_label_type elements.
ensures
- runs [ibegin,iend) through the network and writes the output to the range
at obegin.
- loss_details().to_label() is used to write the network output into
obegin.
!*/
// -------------
const output_label_type& operator() (
const input_type& x
);
/*!
ensures
- runs a single object, x, through the network and returns the output.
- loss_details().to_label() is used to convert the network output into a
output_label_type.
!*/
template <typename iterable_type>
std::vector<output_label_type> operator() (
const iterable_type& data,
size_t batch_size = 128
);
/*!
requires
- batch_size > 0
- data must have a .begin() and .end() that supply iterators over a
sequence of input_type elements. E.g. data could have a type of
std::vector<input_type>
ensures
- runs all the objects in data through the network and returns their
predicted labels. This means this function returns a vector V such that:
- V.size() == data.size()
- for all valid i: V[i] == the predicted label of data[i].
- Elements of data are run through the network in batches of batch_size
items. Using a batch_size > 1 can be faster because it better exploits
the available hardware parallelism.
- loss_details().to_label() is used to convert the network output into a
output_label_type.
!*/
template <typename ...T>
const output_label_type& process (
const input_type& x,
T&& ...args
);
/*!
ensures
- This function is just like (*this)(x), i.e. it runs a single object, x,
through the network and returns the output. But we additionally pass the
given args to loss_details().to_label() as the 4th argument (or more,
depending on how many things are in args) when converting the network
output to an output_label_type. This is useful, for instance, with loss
layers like loss_mmod_ which has an optional adjust_threshold argument to
to_label() that adjusts the detection threshold. Therefore, for such
networks you could call them like: net.process(some_image, -0.5), and -0.5
would be passed so the adjust_threshold argument of to_tensor().
!*/
template <typename iterable_type, typename ...T>
std::vector<output_label_type> process_batch (
const iterable_type& data,
size_t batch_size,
T&& ...args
);
/*!
requires
- batch_size > 0
- data must have a .begin() and .end() that supply iterators over a
sequence of input_type elements. E.g. data could have a type of
std::vector<input_type>
ensures
- This function is just like (*this)(data,batch_size), i.e. it runs a
bunch of objects through the network and returns the outputs. But we
additionally pass the given args to loss_details().to_label() as the 4th
argument (or more, depending on how many things are in args) when
converting the network output to output_label_types. This is useful,
for instance, with loss layers like loss_mmod_ which has an optional
adjust_threshold argument to to_label() that adjusts the detection
threshold. Therefore, for such networks you could call them like:
net.process_batch(std::vector<image_type>({some_image, another_image}), 128, -0.5),
and -0.5 would be passed so the adjust_threshold argument of to_tensor().
!*/
// -------------
template <typename label_iterator>
double compute_loss (
const tensor& x,
label_iterator lbegin
);
/*!
requires
- sample_expansion_factor() != 0
(i.e. to_tensor() must have been called to set sample_expansion_factor()
to something non-zero.)
- x.num_samples()%sample_expansion_factor() == 0
- x.num_samples() > 0
- lbegin == iterator pointing to the start of a range of
x.num_samples()/sample_expansion_factor() training_label_type elements.
ensures
- runs x through the network, compares the output to the expected output
pointed to by lbegin, and returns the resulting loss.
- for all valid k:
- the expected label of the kth sample in x is *(lbegin+k/sample_expansion_factor()).
- This function does not update the network parameters.
- For sub-layers that are immediate inputs into the loss layer, we also populate the
sub-layer's get_gradient_input() tensor with the gradient of the loss with respect
to the sub-layer's output.
!*/
template <typename forward_iterator, typename label_iterator>
double compute_loss (
forward_iterator ibegin,
forward_iterator iend,
label_iterator lbegin
);
/*!
requires
- [ibegin, iend) is an iterator range over input_type objects.
- std::distance(ibegin,iend) > 0
- lbegin == iterator pointing to the start of a range of
std::distance(ibegin,iend) training_label_type elements.
ensures
- runs [ibegin,iend) through the network, compares the output to the
expected output pointed to by lbegin, and returns the resulting loss.
- for all valid k:
- the expected label of *(ibegin+k) is *(lbegin+k).
- This function does not update the network parameters.
- For sub-layers that are immediate inputs into the loss layer, we also populate the
sub-layer's get_gradient_input() tensor with the gradient of the loss with respect
to the sub-layer's output.
!*/
// -------------
double compute_loss (
const tensor& x
);
/*!
requires
- LOSS_DETAILS is an unsupervised loss. i.e. training_label_type==no_label_type.
- sample_expansion_factor() != 0
(i.e. to_tensor() must have been called to set sample_expansion_factor()
to something non-zero.)
- x.num_samples()%sample_expansion_factor() == 0
- x.num_samples() > 0
ensures
- runs x through the network and returns the resulting loss.
- This function does not update the network parameters.
- For sub-layers that are immediate inputs into the loss layer, we also populate the
sub-layer's get_gradient_input() tensor with the gradient of the loss with respect
to the sub-layer's output.
!*/
template <typename forward_iterator>
double compute_loss (
forward_iterator ibegin,
forward_iterator iend,
);
/*!
requires
- LOSS_DETAILS is an unsupervised loss. i.e. training_label_type==no_label_type.
- [ibegin, iend) is an iterator range over input_type objects.
- std::distance(ibegin,iend) > 0
ensures
- runs [ibegin,iend) through the network and returns the resulting loss.
- This function does not update the network parameters.
- For sub-layers that are immediate inputs into the loss layer, we also populate the
sub-layer's get_gradient_input() tensor with the gradient of the loss with respect
to the sub-layer's output.
!*/
// -------------
template <typename label_iterator>
double compute_parameter_gradients (
const tensor& x,
label_iterator lbegin
);
/*!
requires
- sample_expansion_factor() != 0
(i.e. to_tensor() must have been called to set sample_expansion_factor()
to something non-zero.)
- x.num_samples()%sample_expansion_factor() == 0
- x.num_samples() > 0
- lbegin == iterator pointing to the start of a range of
x.num_samples()/sample_expansion_factor() training_label_type elements.
ensures
- runs x through the network, compares the output to the expected output
pointed to by lbegin, and computes parameter and data gradients with
respect to the loss, via backpropagation. Specifically, this function
updates get_final_data_gradient() and also, for each layer, the tensor
returned by get_parameter_gradient().
- for all valid k:
- the expected label of the kth sample in x is *(lbegin+k/sample_expansion_factor()).
- returns compute_loss(x,lbegin)
!*/
template <typename forward_iterator, typename label_iterator>
double compute_parameter_gradients (
forward_iterator ibegin,
forward_iterator iend,
label_iterator lbegin
);
/*!
requires
- [ibegin, iend) is an iterator range over input_type objects.
- std::distance(ibegin,iend) > 0
- lbegin == iterator pointing to the start of a range of
std::distance(ibegin,iend) training_label_type elements.
ensures
- runs [ibegin,iend) through the network, compares the output to the
expected output pointed to by lbegin, and computes parameter and data
gradients with respect to the loss, via backpropagation. Specifically,
this function updates get_final_data_gradient() and also, for each layer,
the tensor returned by get_parameter_gradient().
- for all valid k:
- the expected label of *(ibegin+k) is *(lbegin+k).
- returns compute_loss(ibegin,iend,lbegin)
!*/
double compute_parameter_gradients (
const tensor& x
);
/*!
requires
- LOSS_DETAILS is an unsupervised loss. i.e. training_label_type==no_label_type.
- sample_expansion_factor() != 0
(i.e. to_tensor() must have been called to set sample_expansion_factor()
to something non-zero.)
- x.num_samples()%sample_expansion_factor() == 0
- x.num_samples() > 0
ensures
- runs x through the network and computes parameter and data gradients with
respect to the loss, via backpropagation. Specifically, this function
updates get_final_data_gradient() and also, for each layer, the tensor
returned by get_parameter_gradient().
- returns compute_loss(x)
!*/
template <typename forward_iterator>
double compute_parameter_gradients (
forward_iterator ibegin,
forward_iterator iend
);
/*!
requires
- LOSS_DETAILS is an unsupervised loss. i.e. training_label_type==no_label_type.
- [ibegin, iend) is an iterator range over input_type objects.
- std::distance(ibegin,iend) > 0
ensures
- runs [ibegin,iend) through the network and computes parameter and data
gradients with respect to the loss, via backpropagation. Specifically,
this function updates get_final_data_gradient() and also, for each layer,
the tensor returned by get_parameter_gradient().
- returns compute_loss(ibegin,iend)
!*/
template <typename solver_type>
void update_parameters (
sstack<solver_type> solvers,
double learning_rate
);
/*!
requires
- solver_type is an implementation of the EXAMPLE_SOLVER interface defined
in solvers_abstract.h
- compute_parameter_gradients() has been called.
- The given solvers have only ever been used with this network. That
is, if you want to call update_parameters() on some other neural network
object then you must NOT reuse the same solvers object.
- solvers.size() >= num_computational_layers
- 0 < learning_rate <= 1
ensures
- Updates all the parameters in the network. In particular, we pass each
layer's parameter gradient (i.e. the tensor returned by the layer's
get_parameter_gradient() member) through that layer's corresponding
solver object. This produces a parameter delta vector which we add to
the layer's parameters.
- The solvers use the given learning rate.
!*/
template <typename solver_type>
void update_parameters(std::vector<solver_type>& solvers, double learning_rate
) { update_parameters(make_sstack(solvers), learning_rate); }
/*!
Convenience method for calling update_parameters()
!*/
void back_propagate_error(
const tensor& x
);
/*!
requires
- forward(x) was called to forward propagate x though the network.
Moreover, this was the most recent call to forward() and x has not been
subsequently modified in any way.
- subnet().get_gradient_input() has been set equal to the gradient of this network's
output with respect to the loss function (generally this will be done by calling
compute_loss()).
ensures
- Back propagates the error gradient, subnet().get_gradient_input(), through this
network and computes parameter and data gradients, via backpropagation.
Specifically, this function populates get_final_data_gradient() and also,
for each layer, the tensor returned by get_parameter_gradient().
- All elements of #subnet().get_gradient_input() are set to 0.
- have_same_dimensions(#get_final_data_gradient(), x) == true.
- #get_final_data_gradient() contains the gradient of the network with
respect to x.
!*/
void back_propagate_error(
const tensor& x,
const tensor& gradient_input
);
/*!
requires
- forward(x) was called to forward propagate x though the network.
Moreover, this was the most recent call to forward() and x has not been
subsequently modified in any way.
- have_same_dimensions(gradient_input, subnet().get_output()) == true
ensures
- This function is identical to the version of back_propagate_error()
defined immediately above except that it back-propagates gradient_input
through the network instead of subnet().get_gradient_input(). Therefore, this
version of back_propagate_error() is equivalent to performing:
subnet().get_gradient_input() = gradient_input;
back_propagate_error(x);
Except that calling back_propagate_error(x,gradient_input) avoids the
copy and is therefore slightly more efficient.
- All elements of #subnet.get_gradient_input() are set to 0.
- have_same_dimensions(#get_final_data_gradient(), x) == true.
- #get_final_data_gradient() contains the gradient of the network with
respect to x.
!*/
const tensor& get_final_data_gradient(
) const;
/*!
ensures
- if back_propagate_error() has been called to back-propagate a gradient
through this network then you can call get_final_data_gradient() to
obtain the last data gradient computed. That is, this function returns
the gradient of the network with respect to its inputs.
- Note that there is only one "final data gradient" for an entire network,
not one per layer, since there is only one input to the entire network.
!*/
// -------------
void clean (
);
/*!
ensures
- Causes the network to forget about everything but its parameters.
- invokes subnet().clean()
!*/
};
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const add_loss_layer<T,U>& item);
/*!
prints the network architecture to the given output stream.
!*/
template <typename T, typename U>
void serialize(const add_loss_layer<T,U>& item, std::ostream& out);
template <typename T, typename U>
void deserialize(add_loss_layer<T,U>& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <typename ...T>
decorator_repeat_group<T...> repeat_group (
T&& ...args
);
/*!
ensures
- Decorates a group of variables. This is essentially like std::make_tuple()
except it's only purpose is to group variables together so they can be passed
to the repeat object's constructor.
!*/
template <
size_t num,
template<typename> class REPEATED_LAYER,
typename SUBNET
>
class repeat
{
/*!
REQUIREMENTS ON num
- num > 0
REQUIREMENTS ON REPEATED_LAYER
- REPEATED_LAYER must be a template that stacks more layers onto a deep neural
network. For example, if net_type were a network without a loss layer,
then it should be legal to create a deeper network with a type of
REPEATED_LAYER<net_type>.
REQUIREMENTS ON SUBNET
- One of the following must be true:
- SUBNET is an add_layer object.
- SUBNET is an add_tag_layer object.
- SUBNET is an add_skip_layer object.
- SUBNET is a repeat object.
WHAT THIS OBJECT REPRESENTS
This object adds more layers to a deep neural network. In particular, it
adds REPEATED_LAYER on top of SUBNET num times. So for example, if num were 2 then
repeat<2,REPEATED_LAYER,SUBNET> would create a network equivalent to REPEATED_LAYER<REPEATED_LAYER<SUBNET>>.
Also, this object provides an interface identical to the one defined by the
add_layer object except that we add the num_repetitions() and
get_repeated_layer() methods. These additions are shown below along with
some additional explanatory comments.
!*/
public:
typedef SUBNET subnet_type;
typedef typename SUBNET::input_type input_type;
const static size_t num_computational_layers = (REPEATED_LAYER<SUBNET>::num_computational_layers-SUBNET::num_computational_layers)*num + SUBNET::num_computational_layers;
const static size_t num_layers = (REPEATED_LAYER<SUBNET>::num_layers-SUBNET::num_layers)*num + SUBNET::num_layers;
typedef REPEATED_LAYER<an_unspecified_input_type> repeated_layer_type;
template <typename T, typename ...U>
repeat(
T arg1,
U ...args2
);
/*!
ensures
- arg1 is used to initialize the num_repetitions() copies of REPEATED_LAYER inside
this object. That is, all the REPEATED_LAYER elements are initialized identically
by being given copies of arg1.
- The rest of the arguments to the constructor, i.e. args2, are passed to
SUBNET's constructor.
!*/
template <typename ...T, typename ...U>
repeat(
decorator_repeat_group<T...>&& arg1,
U ...args2
);
/*!
ensures
- arg1 is used to initialize the num_repetitions() copies of REPEATED_LAYER inside
this object. That is, all the REPEATED_LAYER elements are initialized identically
by being given copies of an undecorated arg1.
- The rest of the arguments to the constructor, i.e. args2, are passed to
SUBNET's constructor.
!*/
size_t num_repetitions (
) const;
/*!
ensures
- returns num (i.e. the number of times REPEATED_LAYER was stacked on top of SUBNET)
!*/
const repeated_layer_type& get_repeated_layer (
size_t i
) const;
/*!
requires
- i < num_repetitions()
ensures
- returns a reference to the i-th instance of REPEATED_LAYER. For example,
get_repeated_layer(0) returns the instance of REPEATED_LAYER that is on the top of
the network while get_repeated_layer(num_repetitions()-1) returns the
instance of REPEATED_LAYER that is stacked immediately on top of SUBNET.
!*/
repeated_layer_type& get_repeated_layer (
size_t i
);
/*!
requires
- i < num_repetitions()
ensures
- returns a reference to the i-th instance of REPEATED_LAYER. For example,
get_repeated_layer(0) returns the instance of REPEATED_LAYER that is on the top of
the network while get_repeated_layer(num_repetitions()-1) returns the
instance of REPEATED_LAYER that is stacked immediately on top of SUBNET.
!*/
const subnet_type& subnet(
) const;
/*!
ensures
- returns the SUBNET base network that repeat sits on top of. If you want
to access the REPEATED_LAYER components then you must use get_repeated_layer().
!*/
subnet_type& subnet(
);
/*!
ensures
- returns the SUBNET base network that repeat sits on top of. If you want
to access the REPEATED_LAYER components then you must use get_repeated_layer().
!*/
};
template < size_t num, template<typename> class T, typename U >
std::ostream& operator<<(std::ostream& out, const repeat<num,T,U>& item);
/*!
prints the network architecture to the given output stream.
!*/
template < size_t num, template<typename> class T, typename U >
void serialize(const repeat<num,T,U>& item, std::ostream& out);
template < size_t num, template<typename> class T, typename U >
void deserialize(repeat<num,T,U>& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
template <
unsigned long ID,
typename SUBNET
>
class add_tag_layer
{
/*!
REQUIREMENTS ON SUBNET
- One of the following must be true:
- SUBNET implements the EXAMPLE_INPUT_LAYER interface defined in
input_abstract.h.
- SUBNET is an add_layer object.
- SUBNET is an add_tag_layer object.
- SUBNET is an add_skip_layer object.
- SUBNET is a repeat object.
WHAT THIS OBJECT REPRESENTS
This object adds a new layer to a deep neural network. However, this layer
simply performs the identity transform. This means it is a no-op and its
presence does not change the behavior of the network. It exists solely to
be used by add_skip_layer to reference a particular part of a network.
Also, this object provides an interface identical to the one defined by the
add_layer object.
!*/
};
template <unsigned long ID, typename U>
std::ostream& operator<<(std::ostream& out, const add_tag_layer<ID,U>& item);
/*!
prints the network architecture to the given output stream.
!*/
template <unsigned long ID, typename U>
void serialize(const add_tag_layer<ID,U>& item, std::ostream& out);
template <unsigned long ID, typename U>
void deserialize(add_tag_layer<ID,U>& item, std::istream& in);
/*!
provides serialization support
!*/
template <typename SUBNET> using tag1 = add_tag_layer< 1, SUBNET>;
template <typename SUBNET> using tag2 = add_tag_layer< 2, SUBNET>;
template <typename SUBNET> using tag3 = add_tag_layer< 3, SUBNET>;
template <typename SUBNET> using tag4 = add_tag_layer< 4, SUBNET>;
template <typename SUBNET> using tag5 = add_tag_layer< 5, SUBNET>;
template <typename SUBNET> using tag6 = add_tag_layer< 6, SUBNET>;
template <typename SUBNET> using tag7 = add_tag_layer< 7, SUBNET>;
template <typename SUBNET> using tag8 = add_tag_layer< 8, SUBNET>;
template <typename SUBNET> using tag9 = add_tag_layer< 9, SUBNET>;
template <typename SUBNET> using tag10 = add_tag_layer<10, SUBNET>;
template <template<typename SUBNET> class tag>
struct tag_id
{
/*!
REQUIREMENTS ON tag
Tag should be an add_tag_layer template such as tag1, tag2, etc.
WHAT THIS OBJECT REPRESENTS
This is a tool for finding the numeric ID of a tag layer. For example,
tag_id<tag3>::id == 3.
!*/
const static unsigned long id;
};
// ----------------------------------------------------------------------------------------
template <
template<typename> class TAG_TYPE,
typename SUBNET
>
class add_skip_layer
{
/*!
REQUIREMENTS ON SUBNET
- One of the following must be true:
- SUBNET is an add_layer object.
- SUBNET is an add_tag_layer object.
- SUBNET is an add_skip_layer object.
- SUBNET is a repeat object.
WHAT THIS OBJECT REPRESENTS
This object adds a new layer to a deep neural network which draws its
inputs from layer<TAG_TYPE>(subnet()) and performs the identity transform.
Also, this object provides an interface identical to the one defined by the
add_layer object.
!*/
};
template <template<typename> class T, typename U>
std::ostream& operator<<(std::ostream& out, const add_skip_layer<T,U>& item);
/*!
prints the network architecture to the given output stream.
!*/
template <template<typename> class T, typename U>
void serialize(const add_skip_layer<T,U>& item, std::ostream& out);
template <template<typename> class T, typename U>
void deserialize(add_skip_layer<T,U>& item, std::istream& in);
/*!
provides serialization support
!*/
template <typename SUBNET> using skip1 = add_skip_layer< tag1, SUBNET>;
template <typename SUBNET> using skip2 = add_skip_layer< tag2, SUBNET>;
template <typename SUBNET> using skip3 = add_skip_layer< tag3, SUBNET>;
template <typename SUBNET> using skip4 = add_skip_layer< tag4, SUBNET>;
template <typename SUBNET> using skip5 = add_skip_layer< tag5, SUBNET>;
template <typename SUBNET> using skip6 = add_skip_layer< tag6, SUBNET>;
template <typename SUBNET> using skip7 = add_skip_layer< tag7, SUBNET>;
template <typename SUBNET> using skip8 = add_skip_layer< tag8, SUBNET>;
template <typename SUBNET> using skip9 = add_skip_layer< tag9, SUBNET>;
template <typename SUBNET> using skip10 = add_skip_layer<tag10, SUBNET>;
// ----------------------------------------------------------------------------------------
template <
unsigned int i,
typename net_type
>
auto& layer (
net_type& n
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- i < net_type::num_layers
ensures
- This function allows you to access any layer in a network by its layer index
i. Therefore, it will walk i steps down the network and return the layer
object there. Since networks can be big, the best way to find layer index
numbers is to print a network to the screen since the print out will include
indexes for each layer.
- In general, this function chains together i calls to n.subnet() and returns
the result. So for example:
- if (i == 0)
- returns n
- else if (i == 1)
- returns n.subnet()
- else if (i == 2)
- returns n.subnet().subnet()
- else if (i == 3)
- returns n.subnet().subnet().subnet()
- else
- etc.
Except that when it hits a repeat layer it recurses into the repeated layers
contained inside. That is, if the layer index indicates a layer in a repeat
object this function will make the appropriate call to get_repeated_layer()
and do the right thing.
!*/
template <
template<typename> class Match,
typename net_type
>
auto& layer (
net_type& n
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
ensures
- returns the first layer in n that is of type Match. E.g. if net_type is
fc<relu<fc<input<sample_type>>>> then calling layer<relu>(n) would return
layer<1>(n), that is, a reference to the relu layer.
!*/
template <
template<typename> class Match,
unsigned int i,
typename net_type
>
auto& layer (
net_type& n
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
ensures
- returns layer<i>(layer<Match>(n))
!*/
// ----------------------------------------------------------------------------------------
template <typename net_type>
auto& input_layer (
net_type& net
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
ensures
- returns the input later of the given network object. Specifically, this
function is equivalent to calling:
layer<net_type::num_layers-1>(net);
That is, you get the input layer details object for the network.
!*/
// ----------------------------------------------------------------------------------------
template <
typename net_type,
typename visitor
>
void visit_layer_parameters(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(size_t idx, tensor& t)
or:
v(tensor& t)
ensures
- Loops over all the computational layers (i.e. layers with parameters, as
opposed to loss, tag, or input layers) in net and passes their parameters to
v(). To be specific, this function essentially performs the following:
size_t computational_layer_idx = 0;
for (size_t i = 0; i < net_type::num_layers; ++i)
{
if (layer<i>(net) is a computational layer)
{
v(computational_layer_idx, layer<i>(net).layer_details().get_layer_params());
++computational_layer_idx;
}
}
- When v() is called, the first argument is always < net_type::num_computational_layers.
!*/
// ----------------------------------------------------------------------------------------
template <
typename net_type,
typename visitor
>
void visit_layer_parameter_gradients(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(size_t idx, tensor& t)
or:
v(tensor& t)
ensures
- Loops over all the computational layers (i.e. layers with parameters, as
opposed to loss, tag, or input layers) in net and passes their parameter
gradients to v(). To be specific, this function essentially performs the
following:
size_t computational_layer_idx = 0;
for (size_t i = 0; i < net_type::num_layers; ++i)
{
if (layer<i>(net) is a computational layer)
{
v(computational_layer_idx, layer<i>(net).get_parameter_gradient());
++computational_layer_idx;
}
}
- When v() is called, the first argument is always < net_type::num_computational_layers.
!*/
// ----------------------------------------------------------------------------------------
template <
typename net_type,
typename visitor
>
void visit_layers(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(size_t idx, any_net_type& t)
or:
v(any_net_type& t)
That is, it takes an optional size_t and then any of the network types such as
add_layer, add_loss_layer, etc.
ensures
- Loops over all the layers in net and calls v() on them. To be specific, this
function essentially performs the following:
for (size_t i = 0; i < net_type::num_layers; ++i)
v(i, layer<i>(net));
!*/
template <
typename net_type,
typename visitor
>
void visit_layers_backwards(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(size_t idx, any_net_type& t)
or:
v(any_net_type& t)
That is, it takes an optional size_t and then any of the network types such as
add_layer, add_loss_layer, etc.
ensures
- Loops over all the layers in net and calls v() on them. The loop happens in
the reverse order of visit_layers(). To be specific, this function
essentially performs the following:
for (size_t i = net_type::num_layers; i != 0; --i)
v(i-1, layer<i-1>(net));
!*/
// ----------------------------------------------------------------------------------------
template <
typename net_type,
typename visitor
>
void visit_computational_layers(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(size_t idx, any_computational_layer& t)
or:
v(any_computational_layer& t)
That is, it takes an optional size_t and then any of the computational layers. E.g.
one of the layer types defined in dlib/dnn/layers_abstract.h like fc_ or conv_.
ensures
- Loops over all the computational layers in net and calls v() on them. To be specific, this
function essentially performs the following:
for (size_t i = 0; i < net_type::num_layers; ++i)
if (layer<i>(net) is an add_layer type, i.e. it adds a computational layer)
v(i, layer<i>(net).layer_details());
!*/
template <
size_t begin,
size_t end,
typename net_type,
typename visitor
>
void visit_computational_layers_range(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(size_t idx, any_computational_layer& t)
or:
v(any_computational_layer& t)
That is, it takes an optional size_t and then any of the computational layers. E.g.
one of the layer types defined in dlib/dnn/layers_abstract.h like fc_ or conv_.
ensures
- Loops over all the computational layers in the range [begin,end) in net and calls v()
on them. To be specific, this function essentially performs the following:
for (size_t i = begin; i < end; ++i)
if (layer<i>(net) is an add_layer type, i.e. it adds a computational layer)
v(i, layer<i>(net).layer_details());
!*/
// ----------------------------------------------------------------------------------------
template <
size_t begin,
size_t end,
typename net_type,
typename visitor
>
void visit_layers_range(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(size_t idx, any_net_type& t)
or:
v(any_net_type& t)
That is, it takes an optional size_t and then any of the network types such as
add_layer, add_loss_layer, etc.
- begin <= end <= net_type::num_layers
ensures
- Loops over the layers in the range [begin,end) in net and calls v() on them.
To be specific, this function essentially performs the following:
for (size_t i = begin; i < end; ++i)
v(i, layer<i>(net));
!*/
template <
size_t begin,
size_t end,
typename net_type,
typename visitor
>
void visit_layers_backwards_range(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(size_t idx, any_net_type& t)
or:
v(any_net_type& t)
That is, it takes an optional size_t and then any of the network types such as
add_layer, add_loss_layer, etc.
- begin <= end <= net_type::num_layers
ensures
- Loops over the layers in the range [begin,end) in net and calls v() on them.
The loop happens in the reverse order of visit_layers_range(). To be specific,
this function essentially performs the following:
for (size_t i = end; i != begin; --i)
v(i-1, layer<i-1>(net));
!*/
// ----------------------------------------------------------------------------------------
template <
unsigned long tag_id,
typename net_type,
typename visitor
>
void visit_layers_until_tag(
net_type& net,
visitor v
);
/*!
requires
- net_type is an object of type add_layer, add_loss_layer, add_skip_layer, or
add_tag_layer.
- v is a function object with a signature equivalent to:
v(any_net_type& t)
That is, it must take any of the network types such as add_layer,
add_loss_layer, etc.
ensures
- Loops over all the layers in net beginning with layer<0>(net) and going until
a tag layer with an ID of tag_id is encountered. To be specific, this
function essentially performs the following:
size_t i = 0;
while(layer<i>(net) isn't an add_tag_layer with ID == tag_id) {
v(layer<i>(net));
++i;
}
v(layer<i>(net)); // also visits the tag layer itself at the very end.
!*/
// ----------------------------------------------------------------------------------------
struct layer_test_results
{
std::string log;
bool was_good;
operator bool() const { return was_good; }
};
inline std::ostream& operator<< (std::ostream& out, const layer_test_results& item)
{
out << item.log;
return out;
}
template <
typename layer_details_type
>
layer_test_results test_layer (
layer_details_type l
);
/*!
ensures
- Checks if l correctly implements the EXAMPLE_COMPUTATIONAL_LAYER_ interface
defined in layers_abstract.h. Importantly, it computes numerical approximations
to the gradients and compares them to the outputs of the layer.
- The results of the testing are returned. In particular, if the returned object
is RESULT then we will have:
- RESULT.was_good == false if and only if the layer failed the testing.
- RESULT.log == a string describing why the testing failed if was_good==false.
- Note that this function is only capable of checking layers that take
arbitrary subnetworks as input. So if you have designed a layer that expects
only a certain restricted type of subnetwork then you might get a compile or
runtime error when you call this function.
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_DNn_CORE_ABSTRACT_H_
|