| 1823 | } |
| 1824 | |
| 1825 | bool InputQuery::constructSoftmaxLayer( NLR::NetworkLevelReasoner *nlr, |
| 1826 | Map<unsigned, unsigned> &handledVariableToLayer, |
| 1827 | unsigned newLayerIndex, |
| 1828 | Set<NonlinearConstraint *> &handledNLConstraints ) |
| 1829 | { |
| 1830 | INPUT_QUERY_LOG( "Attempting to construct SoftmaxLayer..." ); |
| 1831 | struct NeuronInformation |
| 1832 | { |
| 1833 | public: |
| 1834 | NeuronInformation( unsigned variable, |
| 1835 | unsigned neuron, |
| 1836 | const Vector<unsigned> &sourceVariables ) |
| 1837 | : _variable( variable ) |
| 1838 | , _neuron( neuron ) |
| 1839 | , _sourceVariables( sourceVariables ) |
| 1840 | { |
| 1841 | } |
| 1842 | |
| 1843 | unsigned _variable; |
| 1844 | unsigned _neuron; |
| 1845 | Vector<unsigned> _sourceVariables; |
| 1846 | }; |
| 1847 | |
| 1848 | List<NeuronInformation> newNeurons; |
| 1849 | |
| 1850 | // Look for Softmaxes where all the element variables have already been handled |
| 1851 | const List<NonlinearConstraint *> &nlConstraints = getNonlinearConstraints(); |
| 1852 | |
| 1853 | unsigned currentSourceLayer = 0; |
| 1854 | for ( const auto &nlc : nlConstraints ) |
| 1855 | { |
| 1856 | if ( handledNLConstraints.exists( nlc ) ) |
| 1857 | continue; |
| 1858 | |
| 1859 | // Only consider Softmax |
| 1860 | if ( nlc->getType() != SOFTMAX ) |
| 1861 | continue; |
| 1862 | |
| 1863 | const SoftmaxConstraint *softmax = (const SoftmaxConstraint *)nlc; |
| 1864 | |
| 1865 | // Have all input variables been handled? |
| 1866 | bool missingInput = false; |
| 1867 | bool sourceLayerDiffers = false; |
| 1868 | for ( const auto &input : softmax->getInputs() ) |
| 1869 | { |
| 1870 | if ( !handledVariableToLayer.exists( input ) ) |
| 1871 | { |
| 1872 | missingInput = true; |
| 1873 | break; |
| 1874 | } |
| 1875 | else if ( _ensureSameSourceLayerInNLR && newNeurons.size() && |
| 1876 | handledVariableToLayer[input] != currentSourceLayer ) |
| 1877 | { |
| 1878 | sourceLayerDiffers = true; |
| 1879 | break; |
| 1880 | } |
| 1881 | } |
| 1882 |
nothing calls this directly
no test coverage detected