The API in TensorFlow 1.0 is not fully backward compatible. That is, a TensorFlow program using TensorFlow 0.n May not work with TensorFlow 1.0. We have made changes to a portion of the API to ensure a consistent internal API style and do not intend to make any reverse changes throughout the 1.n cycle.
This guide will guide you through the major API changes and how to automatically upgrade TensorFlow 1.0 programs. This guide will not only help you with the changes, but also explain why we made them.
How to upgrade
If you want to automatically port your code to version 1.0, you can try our tf_upgrade.py script. Although this script can handle most cases, manual modifications are sometimes required. The script is available on GitHub.
To convert a single 0.n TensorFlow source file to version 1.0, enter a command in the following format:
python tf_upgrade.py --infile InputFile --outfile OutputFileCopy the code
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:
python tf_upgrade.py --infile test.py --outfile test_1. 0.pyCopy the code
The tf_upgrade.py script also generates a file called report.txt, which details all the changes it has made and provides additional suggestions for changes you may need to make manually.
To upgrade the entire directory of the 0.n TensorFlow program to 1.0, type the following command:
python tf_upgrade.py --intree InputDir --outtree OutputDirCopy the code
For example, the following command converts all 0.n TensorFlow programs in the /home/user/cool directory to create their 1.0 equivalents in the /home/user/cool_0 directory:
python tf_upgrade.py --intree /home/user/cool --outtree /home/user/cool_1.0Copy the code
limit
There are a few things to note. In particular:
- You have to manually fix anything about it
tf.reverse()
The instance.tf_upgrade.py
The script will instdout
和report.txt
Warning information is given in the file. - Among the reordered parameters,
tf_upgrade.py
Try to minimize the code that needs to be adjusted so that the actual parameter order cannot be changed automatically. On the contrary,tf_upgrade.py
Make function calls independent by introducing keyword arguments. - As to the
tf.get_variable_scope().reuse_variables()
Such API refactoring probably won’t work, so we recommend removing these functions and replacing them with the following:
with tf.variable_scope(tf.get_variable_scope(), reuse=True):
...Copy the code
- Similar to the
tf.pack
和tf.unpack
And we willTensorArray.pack
和TensorArray.unpack
renameTensorArray.unstack
. However,TensorArray.pack
和TensorArray.unpack
Cannot be detected because they are associated withtf
Namespace profiles are related, for examplefoo = tf.TensorArray(); foo.unpack()
Manually update your code
In addition to running the tf_upgrade.py script, we can also upgrade our code manually. The rest of this article provides a comprehensive list of all backward incompatible changes in TensorFlow 1.0.
Variables
Variable functions have become more consistent and less confusing.
-
tf.VARIABLES
- I should rename it theta
tf.GLOBAL_VARIABLES
- I should rename it theta
-
tf.all_variables
- I should rename it theta
tf.global_variables
- I should rename it theta
-
tf.initialize_all_variables
- I should rename it theta
tf.global_variables_initializer
- I should rename it theta
-
tf.initialize_local_variables
- I should rename it theta
tf.local_variables_initializer
- I should rename it theta
-
tf.initialize_variables
- I should rename it theta
tf.variables_initializer
- I should rename it theta
Summary functions
Summary functions have been merged into the Tf.summary namespace.
-
tf.audio_summary
- I should rename it theta
tf.summary.audio
- I should rename it theta
-
tf.contrib.deprecated.histogram_summary
- I should rename it theta
tf.summary.histogram
- I should rename it theta
-
tf.contrib.deprecated.scalar_summary
- I should rename it theta
tf.summary.scalar
- I should rename it theta
-
tf.histogram_summary
- I should rename it theta
tf.summary.histogram
- I should rename it theta
-
tf.image_summary
- I should rename it theta
tf.summary.image
- I should rename it theta
-
tf.merge_all_summaries
- I should rename it theta
tf.summary.merge_all
- I should rename it theta
-
tf.merge_summary
- I should rename it theta
tf.summary.merge
- I should rename it theta
-
tf.scalar_summary
- I should rename it theta
tf.summary.scalar
- I should rename it theta
-
tf.train.SummaryWriter
- I should rename it theta
tf.summary.FileWriter
- I should rename it theta
The numerical difference
Integer parts and tf.floordiv now use floor semantics. This is to make the structure of Np. divide and Np. mod consistent with tf.Divide and tF.mod, respectively. In addition, we changed the ingestion algorithm used by TF.round to match NumPy.
-
tf.div
-
tf.divide
The semantics of partitioning have been changed to fully match Python semantics. That is, in Python 3/
You get the full value, whereas in Python 2 you only get the integer part, and//
I’m going to get the integer part. However, even thoughtf.div
Floor integer division is also generated. To enforce C-style truncation semantics, you must usetf.truncatediv
Function. - Consider changing your code to use it
tf.divide
, this function has been updated to follow Python semantics.
-
-
tf.mod
-
tf.mod
The semantics of have been changed to match Python semantics. In particular, floor semantics are used for integers. If you want a C-style truncation mod (remainder), you can use ittf.truncatemod
.
-
You can use this table to summarize the old and new functions:
The old and new rounded behaviors can be summarized as follows:
NumPy Match name
Many functions have been renamed to match NumPy. This is done to make the conversion between NumPy and TensorFlow as simple as possible. There are also many cases of feature mismatches, so this is far from a simple and quick rule, but we have removed several common inconsistent functions.
-
tf.inv
- I should rename it theta
tf.reciprocal
. - This was done to avoid contact with
NumPy
The matrix inversenp.inv
Confusion.
- I should rename it theta
-
tf.list_diff
- I should rename it theta
tf.setdiff1d
- I should rename it theta
-
tf.listdiff
- I should rename it theta
tf.setdiff1d
- I should rename it theta
-
tf.mul
- I should rename it theta
tf.multiply
- I should rename it theta
-
tf.neg
- I should rename it theta
tf.negative
- I should rename it theta
-
tf.select
- I should rename it theta
tf.where
-
tf.where
Now you can accept 3 or 1 parameters, andnp.where
The same
- I should rename it theta
-
tf.sub
- I should rename it theta
tf.subtract
- I should rename it theta
NumPy matching parameter
Parameters in some TensorFlow 1.0 methods now match parameters in some NumPy methods. To achieve this, TensorFlow 1.0 has changed the keyword arguments and rearranged some of the arguments. It is worth noting that TensorFlow 1.0 now uses the concept of “axes” rather than “dimensions”. TensorFlow 1.0 is designed to keep tensor parameters first modified for Tensors operations. (See tf.concat changes)
-
tf.argmax
- The key parameters
dimension
Should be modified toaxis
- The key parameters
-
tf.argmin
- The key parameters
dimension
Should be modified toaxis
- The key parameters
-
tf.concat
- The key parameters
concat_dim
Should be modified toaxis
- The parameters have been reformatted as
tf.concat(values, axis, name = 'concat')
- The key parameters
-
tf.count_nonzero
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.expand_dims
- The key parameters
dim
Should be modified toaxis
- The key parameters
-
tf.reduce_all
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reduce_any
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reduce_join
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reduce_logsumexp
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reduce_max
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reduce_mean
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reduce_min
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reduce_prod
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reduce_sum
- The key parameters
reduction_indices
Should be modified toaxis
- The key parameters
-
tf.reverse
- before
tf.reverse
Using a one-dimensional Boolean array to control the dimension of the transformation, we now use the index of the tensor to control it. - For instance,
tf.reverse(a, [True, False, True])
Now I need to be represented by thetatf.reverse(a, [0, 2])
- before
-
tf.reverse_sequence
- The key parameters
batch_dim
Should be modified tobatch_axis
- The key parameters
seq_dim
Should be modified toseq_axis
- The key parameters
-
tf.sparse_concat
- The key parameters
concat_dim
Should be modified toaxis
- The key parameters
-
tf.sparse_reduce_sum
- The key parameters
reduction_axes
Should be modified toaxis
- The key parameters
-
tf.sparse_reduce_sum_sparse
- The key parameters
reduction_axes
Should be modified toaxis
- The key parameters
-
tf.sparse_split
- The key parameters
split_dim
Should be modified toaxis
- The internal parameters of the function need to be reset to
tf.sparse_split(keyword_required=KeywordRequired(), sp_input=None, num_split=None, axis=None, name=None, split_dim=None)
- The key parameters
-
tf.split
- The key parameters
split_dim
Should be modified toaxis
- The key parameters
num_split
Should be modified tonum_or_size_splits
- The internal parameters of the function need to be reset to
tf.split(value, num_or_size_splits, axis=0, num=None, name='split')
.
- The key parameters
-
tf.squeeze
- The key parameters
squeeze_dims
Should be modified toaxis
- The key parameters
-
tf.svd
- The internal parameters of the function need to be reset to
tf.svd(tensor, full_matirces = False, compute_uv = True, name = None)
.
- The internal parameters of the function need to be reset to
Simplified mathematical variants
The mathematics of the batch has been removed. Functionality is now included in the non-bulk version. Similarly, TF.plex_ABS has moved its functionality to TF.ABS.
-
tf.batch_hand_part
- I should rename it theta
tf.hand_part
- I should rename it theta
-
tf.batch_cholesky
- I should rename it theta
tf.cholesky
- I should rename it theta
-
tf.batch_cholesky_solve
- I should rename it theta
tf.cholesky_solve
- I should rename it theta
-
tf.batch_fft
- I should rename it theta
tf.fft
- I should rename it theta
-
tf.batch_fft3d
- I should rename it theta
tf.fft3d
- I should rename it theta
-
tf.batch_ifft
- I should rename it theta
tf.ifft
- I should rename it theta
-
tf.batch_ifft2d
- I should rename it theta
tf.ifft2d
- I should rename it theta
-
tf.batch_ifft3d
- I should rename it theta
tf.ifft3d
- I should rename it theta
-
tf.batch_matmul
- I should rename it theta
tf.matmul
- I should rename it theta
-
tf.batch_matrix_determinant
- I should rename it theta
tf.matrix_determinant
- I should rename it theta
-
tf.batch_matrix_diag
- I should rename it theta
tf.matrix_diag
- I should rename it theta
-
tf.batch_matrix_inverse
- I should rename it theta
tf.matrix_inverse
- I should rename it theta
-
tf.batch_matrix_solve
- I should rename it theta
tf.matrix_solve
- I should rename it theta
-
tf.batch_matrix_solve_ls
- I should rename it theta
tf.matrix_solve_ls
- I should rename it theta
-
tf.batch_matrix_transpose
- I should rename it theta
tf.matrix_transpose
- I should rename it theta
-
tf.batch_matrix_triangular_solve
- I should rename it theta
tf.matrix_triangular_solve
- I should rename it theta
-
tf.batch_self_adjoint_eig
- I should rename it theta
tf.self_adjoint_eig
- I should rename it theta
-
tf.batch_self_adjoint_eigvals
- I should rename it theta
tf.self_adjoint_eigvals
- I should rename it theta
-
tf.batch_set_diag
- I should rename it theta
tf.set_diag
- I should rename it theta
-
tf.batch_svd
- I should rename it theta
tf.svd
- I should rename it theta
-
tf.complex_abs
- I should rename it theta
tf.abs
- I should rename it theta
Other changes
Several other changes have been made, including:
-
tf.image.per_image_whitening
- I should rename it theta
tf.image.per_image_standardization
- I should rename it theta
-
tf.nn.sigmoid_cross_entropy_with_logits
- The argument to the function has been adjusted to
tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=None)
- The argument to the function has been adjusted to
-
tf.nn.softmax_cross_entropy_with_logits
- The argument to the function has been adjusted to
tf.nn.softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, dim=-1, name=None)
- The argument to the function has been adjusted to
-
tf.nn.sparse_softmax_cross_entropy_with_logits
- The argument to the function has been adjusted to
tf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=None)
- The argument to the function has been adjusted to
-
tf.ones_initializer
- The function has been modified to
tf.ones_initializer()
- The function has been modified to
-
tf.pack
- I should rename it theta
tf.stack
- I should rename it theta
-
tf.round
-
tf.round
The semantics of the Banker now match the rounding.
-
-
tf.unpack
- I should rename it theta
tf.unstack
- I should rename it theta
-
tf.zeros_initializer
- The function has been modified to
tf.zeros_initializer()
- The function has been modified to
Reference: website