Wrapper allowing functional layers to be used with eager execution. When eager execution is enabled Variables get deleted when they go out of scope, and are not stored in global collections by default. A lot of code (mostly the functional layers in tf.layers) assumes that variables are kept i
| 1852 | |
| 1853 | |
| 1854 | class EagerVariableStore(object): |
| 1855 | """Wrapper allowing functional layers to be used with eager execution. |
| 1856 | |
| 1857 | When eager execution is enabled Variables get deleted when they go out of |
| 1858 | scope, and are not stored in global collections by default. A lot of code |
| 1859 | (mostly the functional layers in tf.layers) assumes that variables are kept in |
| 1860 | a global list. |
| 1861 | |
| 1862 | EagerVariableStore can be used in conjunction with this code to make it |
| 1863 | eager-friendly. For example, to create a dense layer, use: |
| 1864 | |
| 1865 | ``` |
| 1866 | container = tfe.EagerVariableStore() |
| 1867 | for input in dataset_iterator: |
| 1868 | with container.as_default(): |
| 1869 | x = tf.compat.v1.layers.dense(input, name="l1") |
| 1870 | print(container.variables) # Should print the variables used in the layer. |
| 1871 | ``` |
| 1872 | """ |
| 1873 | |
| 1874 | def __init__(self, store=None): |
| 1875 | if store is not None: |
| 1876 | if not store._store_eager_variables: # pylint: disable=protected-access |
| 1877 | raise ValueError("Cannot construct EagerVariableStore from a " |
| 1878 | "VariableStore object that does not hold eager " |
| 1879 | "variables.") |
| 1880 | self._store = store |
| 1881 | else: |
| 1882 | self._store = _VariableStore() |
| 1883 | self._store._store_eager_variables = True # pylint: disable=protected-access |
| 1884 | |
| 1885 | def as_default(self): |
| 1886 | return with_variable_store(self._store) |
| 1887 | |
| 1888 | def variables(self): |
| 1889 | return sorted(self._store._vars.values(), key=lambda x: x.name) # pylint: disable=protected-access |
| 1890 | |
| 1891 | def trainable_variables(self): |
| 1892 | # pylint: disable=protected-access |
| 1893 | return sorted([x for x in self._store._vars.values() if x.trainable], |
| 1894 | key=lambda x: x.name) |
| 1895 | # pylint: enable=protected-access |
| 1896 | |
| 1897 | def non_trainable_variables(self): |
| 1898 | # pylint: disable=protected-access |
| 1899 | return sorted([x for x in self._store._vars.values() if not x.trainable], |
| 1900 | key=lambda x: x.name) |
| 1901 | # pylint: enable=protected-access |
| 1902 | |
| 1903 | def copy(self): |
| 1904 | """Copy this variable store and all of its contents. |
| 1905 | |
| 1906 | Variables contained in this store will be copied over to the new variable |
| 1907 | store, meaning that they can be modified without affecting the variables in |
| 1908 | this store. |
| 1909 | |
| 1910 | Returns: |
| 1911 | A new EagerVariableStore instance containing copied variables. |