Use tf.layers instead of tf.contrib.layers

This commit is contained in:
Aurélien Geron
2017-04-30 10:21:27 +02:00
parent 14101abcf9
commit 326d32cae0
7 changed files with 531 additions and 258 deletions

View File

@@ -986,6 +986,18 @@
"Let's create a neural network that will take observations as inputs, and output the action to take for each observation. To choose an action, the network will first estimate a probability for each action, then select an action randomly according to the estimated probabilities. In the case of the Cart-Pole environment, there are just two possible actions (left or right), so we only need one output neuron: it will output the probability `p` of the action 0 (left), and of course the probability of action 1 (right) will be `1 - p`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: instead of using the `fully_connected()` function from the `tensorflow.contrib.layers` module (as in the book), we now use the `dense()` function from the `tf.layers` module, which did not exist when this chapter was written. This is preferable because anything in contrib may change or be deleted without notice, while `tf.layers` is part of the official API. As you will see, the code is mostly the same.\n",
"\n",
"The main differences relevant to this chapter are:\n",
"* the `_fn` suffix was removed in all the parameters that had it (for example the `activation_fn` parameter was renamed to `activation`).\n",
"* the `weights` parameter was renamed to `kernel`,\n",
"* the default activation is `None` instead of `tf.nn.relu`"
]
},
{
"cell_type": "code",
"execution_count": 34,
@@ -997,7 +1009,6 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.contrib.layers import fully_connected\n",
"\n",
"# 1. Specify the network architecture\n",
"n_inputs = 4 # == env.observation_space.shape[0]\n",
@@ -1007,10 +1018,10 @@
"\n",
"# 2. Build the neural network\n",
"X = tf.placeholder(tf.float32, shape=[None, n_inputs])\n",
"hidden = fully_connected(X, n_hidden, activation_fn=tf.nn.elu,\n",
" weights_initializer=initializer)\n",
"outputs = fully_connected(hidden, n_outputs, activation_fn=tf.nn.sigmoid,\n",
" weights_initializer=initializer)\n",
"hidden = tf.layers.dense(X, n_hidden, activation=tf.nn.elu,\n",
" kernel_initializer=initializer)\n",
"outputs = tf.layers.dense(hidden, n_outputs, activation=tf.nn.sigmoid,\n",
" kernel_initializer=initializer)\n",
"\n",
"# 3. Select a random action based on the estimated probabilities\n",
"p_left_and_right = tf.concat(axis=1, values=[outputs, 1 - outputs])\n",
@@ -1121,7 +1132,6 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.contrib.layers import fully_connected\n",
"\n",
"tf.reset_default_graph()\n",
"\n",
@@ -1136,8 +1146,8 @@
"X = tf.placeholder(tf.float32, shape=[None, n_inputs])\n",
"y = tf.placeholder(tf.float32, shape=[None, n_outputs])\n",
"\n",
"hidden = fully_connected(X, n_hidden, activation_fn=tf.nn.elu, weights_initializer=initializer)\n",
"logits = fully_connected(hidden, n_outputs, activation_fn=None)\n",
"hidden = tf.layers.dense(X, n_hidden, activation=tf.nn.elu, kernel_initializer=initializer)\n",
"logits = tf.layers.dense(hidden, n_outputs)\n",
"outputs = tf.nn.sigmoid(logits) # probability of action 0 (left)\n",
"p_left_and_right = tf.concat(axis=1, values=[outputs, 1 - outputs])\n",
"action = tf.multinomial(tf.log(p_left_and_right), num_samples=1)\n",
@@ -1275,7 +1285,6 @@
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.contrib.layers import fully_connected\n",
"\n",
"tf.reset_default_graph()\n",
"\n",
@@ -1289,8 +1298,8 @@
"\n",
"X = tf.placeholder(tf.float32, shape=[None, n_inputs])\n",
"\n",
"hidden = fully_connected(X, n_hidden, activation_fn=tf.nn.elu, weights_initializer=initializer)\n",
"logits = fully_connected(hidden, n_outputs, activation_fn=None)\n",
"hidden = tf.layers.dense(X, n_hidden, activation=tf.nn.elu, kernel_initializer=initializer)\n",
"logits = tf.layers.dense(hidden, n_outputs)\n",
"outputs = tf.nn.sigmoid(logits) # probability of action 0 (left)\n",
"p_left_and_right = tf.concat(axis=1, values=[outputs, 1 - outputs])\n",
"action = tf.multinomial(tf.log(p_left_and_right), num_samples=1)\n",
@@ -1366,7 +1375,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 45,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1416,7 +1425,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 46,
"metadata": {
"collapsed": true,
"deletable": true,
@@ -1429,7 +1438,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 47,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1454,7 +1463,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 48,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1499,7 +1508,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 49,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1594,7 +1603,7 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 50,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1623,7 +1632,7 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 51,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1637,7 +1646,7 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 52,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1650,7 +1659,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 53,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1677,7 +1686,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 54,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1691,7 +1700,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 55,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1704,7 +1713,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 56,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1737,7 +1746,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 57,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1759,7 +1768,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 58,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1790,9 +1799,22 @@
"## Build DQN"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: instead of using `tf.contrib.layers.convolution2d()` or `tf.contrib.layers.conv2d()` (as in the book), we now use the `tf.layers.conv2d()`, which did not exist when this chapter was written. This is preferable because anything in contrib may change or be deleted without notice, while `tf.layers` is part of the official API. As you will see, the code is mostly the same, except that the parameter names have changed slightly:\n",
"* the `num_outputs` parameter was renamed to `filters`,\n",
"* the `stride` parameter was renamed to `strides`,\n",
"* the `_fn` suffix was removed from parameter names that had it (e.g., `activation_fn` was renamed to `activation`),\n",
"* the `weights_initializer` parameter was renamed to `kernel_initializer`,\n",
"* the weights variable was renamed to `\"kernel\"` (instead of `\"weights\"`), and the biases variable was renamed from `\"biases\"` to `\"bias\"`,\n",
"* and the default `activation` is now `None` instead of `tf.nn.relu`."
]
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 59,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1802,8 +1824,6 @@
"source": [
"tf.reset_default_graph()\n",
"\n",
"from tensorflow.contrib.layers import convolution2d, fully_connected\n",
"\n",
"input_height = 88\n",
"input_width = 80\n",
"input_channels = 1\n",
@@ -1824,12 +1844,12 @@
" prev_layer = X_state\n",
" conv_layers = []\n",
" with tf.variable_scope(scope) as scope:\n",
" for n_maps, kernel_size, stride, padding, activation in zip(conv_n_maps, conv_kernel_sizes, conv_strides, conv_paddings, conv_activation):\n",
" prev_layer = convolution2d(prev_layer, num_outputs=n_maps, kernel_size=kernel_size, stride=stride, padding=padding, activation_fn=activation, weights_initializer=initializer)\n",
" for n_maps, kernel_size, strides, padding, activation in zip(conv_n_maps, conv_kernel_sizes, conv_strides, conv_paddings, conv_activation):\n",
" prev_layer = tf.layers.conv2d(prev_layer, filters=n_maps, kernel_size=kernel_size, strides=strides, padding=padding, activation=activation, kernel_initializer=initializer)\n",
" conv_layers.append(prev_layer)\n",
" last_conv_layer_flat = tf.reshape(prev_layer, shape=[-1, n_hidden_inputs])\n",
" hidden = fully_connected(last_conv_layer_flat, n_hidden, activation_fn=hidden_activation, weights_initializer=initializer)\n",
" outputs = fully_connected(hidden, n_outputs, activation_fn=None)\n",
" hidden = tf.layers.dense(last_conv_layer_flat, n_hidden, activation=hidden_activation, kernel_initializer=initializer)\n",
" outputs = tf.layers.dense(hidden, n_outputs)\n",
" trainable_vars = {var.name[len(scope.name):]: var for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope.name)}\n",
" return outputs, trainable_vars\n",
"\n",
@@ -1857,7 +1877,7 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 60,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1870,7 +1890,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 61,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -1896,7 +1916,7 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 62,
"metadata": {
"collapsed": true,
"deletable": true,
@@ -1919,7 +1939,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 63,
"metadata": {
"collapsed": false,
"deletable": true,
@@ -2023,7 +2043,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2+"
"version": "3.5.3"
},
"nav_menu": {},
"toc": {