aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/docs_src/install/migration.md
blob: d6c31f96bd624f03f0b868a030383851c4e48ef7 (plain)
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

# Transitioning to TensorFlow 1.0


The APIs in TensorFlow 1.0 have changed in ways that are not all backwards
compatible.  That is, TensorFlow programs that worked on TensorFlow 0.n won't
necessarily work on TensorFlow 1.0.  We have made this API changes to ensure an
internally-consistent API, and do not plan to make backwards-breaking changes
throughout the 1.N lifecycle.

This guide walks you through the major changes in the API and how to
automatically upgrade your programs for TensorFlow 1.0.  This guide not
only steps you through the changes but also explains why we've made them.

## How to upgrade

If you would like to automatically  port your code to 1.0, you can try our
`tf_upgrade.py` script. While this script handles many cases, manual changes
are sometimes necessary.
  Get this script from our
[GitHub tree](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/compatibility).

To convert a single 0.n TensorFlow source file to 1.0, enter a
command of the following format:

<pre>
$ <b>python tf_upgrade.py --infile</b> <i>InputFile</i> <b>--outfile</b> <i>OutputFile</i>
</pre>

For example, the following command converts a 0.n TensorFlow
program named `test.py` to a 1.0 TensorFlow program named `test_1.0.py`:

<pre>
$ <b>python tf_upgrade.py --infile test.py --outfile test_1.0.py</b>
</pre>

The `tf_upgrade.py` script also generates a file named `report.txt`, which
details all the changes it performed and makes additional suggestions about
changes you might need to make manually.

To upgrade a whole directory of 0.n TensorFlow programs to 1.0,
enter a command having the following format:

<pre>
$ <b>python tf_upgrade.py --intree</b> <i>InputDir</i> <b>--outtree</b> <i>OutputDir</i>
</pre>

For example, the following command converts all the 0.n TensorFlow programs
in the `/home/user/cool` directory, creating their 1.0 equivalents in
the `/home/user/cool_1.0` directory:

<pre>
$ <b>python tf_upgrade.py --intree /home/user/cool --outtree /home/user/cool_1.0</b>
</pre>

### Limitations

There are a few things to watch out for. Specifically:

 * You must manually fix any instances of `tf.reverse()`.
   The `tf_upgrade.py` script will warn you about `tf.reverse()` in
   stdout and in the `report.txt` file.
 * On reordered arguments, `tf_upgrade.py` tries to minimally reformat
   your code, so it cannot automatically change the actual argument order.
   Instead, `tf_upgrade.py` makes your function invocations order-independent
   by introducing keyword arguments.
 * Constructions like `tf.get_variable_scope().reuse_variables()`
   will likely not work. We recommend deleting those lines and replacing
   them with lines such as the following:

   <pre class="prettyprint">
   with tf.variable_scope(tf.get_variable_scope(), reuse=True):
     ...
   </pre>

 * Analogously to `tf.pack` and  `tf.unpack`, we're renamed
   `TensorArray.pack` and `TensorArray.unpack` to
   `TensorArray.stack` and `TensorArray.unstack`. However, `TensorArray.pack`
   and `TensorArray.unpack` cannot be detected lexically since they are
   indirectly related to the `tf` namespace e.g.
   `foo = tf.TensorArray(); foo.unpack()`

## Upgrading your code manually

Instead of running `tf_upgrade.py`, you may manually upgrade your code.
The remainder of this document provides a comprehensive list of
all backward incompatible changes made in TensorFlow 1.0.


### Variables

Variable functions have been made more consistent and less confusing.

* `tf.VARIABLES`
    * should be renamed to `tf.GLOBAL_VARIABLES`
* `tf.all_variables`
    * should be renamed to `tf.global_variables`
* `tf.initialize_all_variables`
    * should be renamed to `tf.global_variables_initializer`
* `tf.initialize_local_variables`
    * should be renamed to `tf.local_variables_initializer`
* `tf.initialize_variables`
    * should be renamed to `tf.variables_initializer`

### Summary functions

Summary functions have been consolidated under the `tf.summary` namespace.

* `tf.audio_summary`
    * should be renamed to `tf.summary.audio`
* `tf.contrib.deprecated.histogram_summary`
    * should be renamed to `tf.summary.histogram`
* `tf.contrib.deprecated.scalar_summary`
    * should be renamed to `tf.summary.scalar`
* `tf.histogram_summary`
    * should be renamed to `tf.summary.histogram`
* `tf.image_summary`
    * should be renamed to `tf.summary.image`
* `tf.merge_all_summaries`
    * should be renamed to `tf.summary.merge_all`
* `tf.merge_summary`
    * should be renamed to `tf.summary.merge`
* `tf.scalar_summary`
    * should be renamed to `tf.summary.scalar`
* `tf.train.SummaryWriter`
    * should be renamed to `tf.summary.FileWriter`

### Numeric differences


Integer division and `tf.floordiv` now uses flooring semantics. This is to
make the results of `np.divide` and `np.mod` consistent with `tf.divide` and
`tf.mod`, respectively. In addition we have changed the rounding algorithm
used by `tf.round` to match NumPy.


* `tf.div`

    * The semantics of `tf.divide` division have been changed to match Python
semantics completely. That is, `/` in Python 3     and future division mode in
Python 2 will produce floating point numbers always, `//` will produce floored
division.     However, even `tf.div` will produce floored integer division.
To force C-style truncation semantics, you must use `tf.truncatediv`.

    * Consider changing your code to use `tf.divide`, which follows Python semantics for promotion.

* `tf.mod`

    * The semantics of `tf.mod` have been changed to match Python semantics. In
particular, flooring semantics are used for     integers. If you wish to have
C-style truncation mod (remainders), you can use `tf.truncatemod`


The old and new behavior of division can be summarized with this table:

| Expr                | TF 0.11 (py2) | TF 0.11 (py3) | TF 1.0 (py2) | TF 1.0 (py3) |
|---------------------|---------------|---------------|--------------|--------------|
| tf.div(3,4)         | 0             | 0             | 0            | 0            |
| tf.div(-3,4)        | 0             | 0             | -1           | -1           |
| tf.mod(-3,4)        | -3            | -3            | 1            | 1            |
| -3/4                | 0             | -0.75         | -1           | -0.75        |
| -3/4tf.divide(-3,4) | N/A           | N/A           | -0.75        | -1           |

The old and new behavior of rounding can be summarized with this table:

| Input | Python | NumPy | C++ round() | TensorFlow 0.11(floor(x+.5)) | TensorFlow 1.0 |
|-------|--------|-------|-------------|------------------------------|----------------|
| -3.5  | -4     | -4    | -4          | -3                           | -4             |
| -2.5  | -2     | -2    | -3          | -2                           | -2             |
| -1.5  | -2     | -2    | -2          | -1                           | -2             |
| -0.5  | 0      | 0     | -1          | 0                            | 0              |
| 0.5   | 0      | 0     | 1           | 1                            | 0              |
| 1.5   | 2      | 2     | 2           | 2                            | 2              |
| 2.5   | 2      | 2     | 3           | 3                            | 2              |
| 3.5   | 4      | 4     | 4           | 4                            | 4              |



### NumPy matching names


Many functions have been renamed to match NumPy. This was done to make the
transition between NumPy and TensorFlow as easy as possible. There are still
numerous cases where functions do not match, so this is far from a hard and
fast rule, but we have removed several commonly noticed inconsistencies.

* `tf.inv`
    * should be renamed to `tf.reciprocal`
    * This was done to avoid confusion with NumPy's matrix inverse `np.inv`
* `tf.list_diff`
    * should be renamed to `tf.setdiff1d`
* `tf.listdiff`
    * should be renamed to `tf.setdiff1d`
* `tf.mul`
    * should be renamed to `tf.multiply`
* `tf.neg`
    * should be renamed to `tf.negative`
* `tf.select`
    * should be renamed to `tf.where`
    * `tf.where` now takes 3 arguments or 1 argument, just like `np.where`
* `tf.sub`
    * should be renamed to `tf.subtract`

### NumPy matching arguments

Arguments for certain TensorFlow 1.0 methods now match arguments in certain
NumPy methods.  To achieve this, TensorFlow 1.0 has changed keyword arguments
and reordered some arguments. Notably, TensorFlow 1.0 now uses `axis` rather
than `dimension`. TensorFlow 1.0 aims to keep the tensor argument first on
operations that modify Tensors. (see the `tf.concat` change).


* `tf.argmax`
    * keyword argument `dimension` should be renamed to `axis`
* `tf.argmin`
    * keyword argument `dimension` should be renamed to `axis`
* `tf.concat`
    * keyword argument `concat_dim` should be renamed to `axis`
    * arguments have been reordered to `tf.concat(values, axis, name='concat')`.
* `tf.count_nonzero`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.expand_dims`
    * keyword argument `dim` should be renamed to `axis`
* `tf.reduce_all`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reduce_any`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reduce_join`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reduce_logsumexp`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reduce_max`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reduce_mean`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reduce_min`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reduce_prod`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reduce_sum`
    * keyword argument `reduction_indices` should be renamed to `axis`
* `tf.reverse`
    * `tf.reverse` used to take a 1D `bool` tensor to control which dimensions were reversed. Now we use a Tensor of axis indices.
    * For example `tf.reverse(a, [True, False, True])` now must be `tf.reverse(a, [0, 2])`
* `tf.reverse_sequence`
    * keyword argument `batch_dim` should be renamed to `batch_axis`
    * keyword argument `seq_dim` should be renamed to `seq_axis`
* `tf.sparse_concat`
    * keyword argument `concat_dim` should be renamed to `axis`
* `tf.sparse_reduce_sum`
    * keyword argument `reduction_axes` should be renamed to `axis`
* `tf.sparse_reduce_sum_sparse`
    * keyword argument `reduction_axes` should be renamed to `axis`
* `tf.sparse_split`
    * keyword argument `split_dim` should be renamed to `axis`
    * arguments have been reordered to `tf.sparse_split(keyword_required=KeywordRequired(), sp_input=None, num_split=None, axis=None, name=None, split_dim=None)`.
* `tf.split`
    * keyword argument `split_dim` should be renamed to `axis`
    * keyword argument `num_split` should be renamed to `num_or_size_splits`
    * arguments have been reordered to `tf.split(value, num_or_size_splits, axis=0, num=None, name='split')`.
* `tf.squeeze`
    * keyword argument `squeeze_dims` should be renamed to `axis`
* `tf.svd`
    * arguments have been reordered to `tf.svd(tensor, full_matrices=False, compute_uv=True, name=None)`.

### Simplified math variants

Batched versions of math operations have been removed. Now the functionality is
contained in the non-batched versions. Similarly,`tf.complex_abs` has had its
functionality moved to `tf.abs`

* `tf.batch_band_part`
    * should be renamed to `tf.band_part`
* `tf.batch_cholesky`
    * should be renamed to `tf.cholesky`
* `tf.batch_cholesky_solve`
    * should be renamed to `tf.cholesky_solve`
* `tf.batch_fft`
    * should be renamed to `tf.fft`
* `tf.batch_fft3d`
    * should be renamed to `tf.fft3d`
* `tf.batch_ifft`
    * should be renamed to `tf.ifft`
* `tf.batch_ifft2d`
    * should be renamed to `tf.ifft2d`
* `tf.batch_ifft3d`
    * should be renamed to `tf.ifft3d`
* `tf.batch_matmul`
    * should be renamed to `tf.matmul`
* `tf.batch_matrix_determinant`
    * should be renamed to `tf.matrix_determinant`
* `tf.batch_matrix_diag`
    * should be renamed to `tf.matrix_diag`
* `tf.batch_matrix_inverse`
    * should be renamed to `tf.matrix_inverse`
* `tf.batch_matrix_solve`
    * should be renamed to `tf.matrix_solve`
* `tf.batch_matrix_solve_ls`
    * should be renamed to `tf.matrix_solve_ls`
* `tf.batch_matrix_transpose`
    * should be renamed to `tf.matrix_transpose`
* `tf.batch_matrix_triangular_solve`
    * should be renamed to `tf.matrix_triangular_solve`
* `tf.batch_self_adjoint_eig`
    * should be renamed to `tf.self_adjoint_eig`
* `tf.batch_self_adjoint_eigvals`
    * should be renamed to `tf.self_adjoint_eigvals`
* `tf.batch_set_diag`
    * should be renamed to `tf.set_diag`
* `tf.batch_svd`
    * should be renamed to `tf.svd`
* `tf.complex_abs`
    * should be renamed to `tf.abs`

### Misc Changes

Several other changes have been made, including the following:

* `tf.image.per_image_whitening`
    * should be renamed to `tf.image.per_image_standardization`
* `tf.nn.sigmoid_cross_entropy_with_logits`
    * arguments have been reordered to `tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=None)`.
* `tf.nn.softmax_cross_entropy_with_logits`
    * arguments have been reordered to `tf.nn.softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, dim=-1, name=None)`.
* `tf.nn.sparse_softmax_cross_entropy_with_logits`
    * arguments have been reordered to `tf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=None)`.
* `tf.ones_initializer`
    * should be changed to a function call i.e. `tf.ones_initializer()`
* `tf.pack`
    * should be renamed to `tf.stack`
* `tf.round`
    * The semantics of `tf.round` now match Banker's rounding.
* `tf.unpack`
    * should be renamed to `tf.unstack`
* `tf.zeros_initializer`
    * should be changed to a function call i.e. `tf.zeros_initializer()`