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 ittf.reverse()The instance.tf_upgrade.pyThe script will instdoutreport.txtWarning information is given in the file.
  • Among the reordered parameters,tf_upgrade.pyTry to minimize the code that needs to be adjusted so that the actual parameter order cannot be changed automatically. On the contrary,tf_upgrade.pyMake function calls independent by introducing keyword arguments.
  • As to thetf.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 thetf.packtf.unpackAnd we willTensorArray.packTensorArray.unpackrenameTensorArray.unstack. However,TensorArray.packTensorArray.unpackCannot be detected because they are associated withtfNamespace 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 thetatf.GLOBAL_VARIABLES
  • tf.all_variables
    • I should rename it thetatf.global_variables
  • tf.initialize_all_variables
    • I should rename it thetatf.global_variables_initializer
  • tf.initialize_local_variables
    • I should rename it thetatf.local_variables_initializer
  • tf.initialize_variables
    • I should rename it thetatf.variables_initializer
Summary functions

Summary functions have been merged into the Tf.summary namespace.

  • tf.audio_summary
    • I should rename it thetatf.summary.audio
  • tf.contrib.deprecated.histogram_summary
    • I should rename it thetatf.summary.histogram
  • tf.contrib.deprecated.scalar_summary
    • I should rename it thetatf.summary.scalar
  • tf.histogram_summary
    • I should rename it thetatf.summary.histogram
  • tf.image_summary
    • I should rename it thetatf.summary.image
  • tf.merge_all_summaries
    • I should rename it thetatf.summary.merge_all
  • tf.merge_summary
    • I should rename it thetatf.summary.merge
  • tf.scalar_summary
    • I should rename it thetatf.summary.scalar
  • tf.train.SummaryWriter
    • I should rename it thetatf.summary.FileWriter
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.divideThe 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.divFloor integer division is also generated. To enforce C-style truncation semantics, you must usetf.truncatedivFunction.
    • Consider changing your code to use ittf.divide, this function has been updated to follow Python semantics.
  • tf.mod
    • tf.modThe 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 thetatf.reciprocal.
    • This was done to avoid contact withNumPyThe matrix inversenp.invConfusion.
  • tf.list_diff
    • I should rename it thetatf.setdiff1d
  • tf.listdiff
    • I should rename it thetatf.setdiff1d
  • tf.mul
    • I should rename it thetatf.multiply
  • tf.neg
    • I should rename it thetatf.negative
  • tf.select
    • I should rename it thetatf.where
    • tf.whereNow you can accept 3 or 1 parameters, andnp.whereThe same
  • tf.sub
    • I should rename it thetatf.subtract
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 parametersdimensionShould be modified toaxis
  • tf.argmin
    • The key parametersdimensionShould be modified toaxis
  • tf.concat
    • The key parametersconcat_dimShould be modified toaxis
    • The parameters have been reformatted astf.concat(values, axis, name = 'concat')
  • tf.count_nonzero
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.expand_dims
    • The key parametersdimShould be modified toaxis
  • tf.reduce_all
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reduce_any
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reduce_join
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reduce_logsumexp
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reduce_max
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reduce_mean
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reduce_min
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reduce_prod
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reduce_sum
    • The key parametersreduction_indicesShould be modified toaxis
  • tf.reverse
    • beforetf.reverseUsing 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])
  • tf.reverse_sequence
    • The key parametersbatch_dimShould be modified tobatch_axis
    • The key parametersseq_dimShould be modified toseq_axis
  • tf.sparse_concat
    • The key parametersconcat_dimShould be modified toaxis
  • tf.sparse_reduce_sum
    • The key parametersreduction_axesShould be modified toaxis
  • tf.sparse_reduce_sum_sparse
    • The key parametersreduction_axesShould be modified toaxis
  • tf.sparse_split
    • The key parameterssplit_dimShould be modified toaxis
    • The internal parameters of the function need to be reset totf.sparse_split(keyword_required=KeywordRequired(), sp_input=None, num_split=None, axis=None, name=None, split_dim=None)
  • tf.split
    • The key parameterssplit_dimShould be modified toaxis
    • The key parametersnum_splitShould be modified tonum_or_size_splits
    • The internal parameters of the function need to be reset totf.split(value, num_or_size_splits, axis=0, num=None, name='split').
  • tf.squeeze
    • The key parameterssqueeze_dimsShould be modified toaxis
  • tf.svd
    • The internal parameters of the function need to be reset totf.svd(tensor, full_matirces = False, compute_uv = True, name = None).
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 thetatf.hand_part
  • tf.batch_cholesky
    • I should rename it thetatf.cholesky
  • tf.batch_cholesky_solve
    • I should rename it thetatf.cholesky_solve
  • tf.batch_fft
    • I should rename it thetatf.fft
  • tf.batch_fft3d
    • I should rename it thetatf.fft3d
  • tf.batch_ifft
    • I should rename it thetatf.ifft
  • tf.batch_ifft2d
    • I should rename it thetatf.ifft2d
  • tf.batch_ifft3d
    • I should rename it thetatf.ifft3d
  • tf.batch_matmul
    • I should rename it thetatf.matmul
  • tf.batch_matrix_determinant
    • I should rename it thetatf.matrix_determinant
  • tf.batch_matrix_diag
    • I should rename it thetatf.matrix_diag
  • tf.batch_matrix_inverse
    • I should rename it thetatf.matrix_inverse
  • tf.batch_matrix_solve
    • I should rename it thetatf.matrix_solve
  • tf.batch_matrix_solve_ls
    • I should rename it thetatf.matrix_solve_ls
  • tf.batch_matrix_transpose
    • I should rename it thetatf.matrix_transpose
  • tf.batch_matrix_triangular_solve
    • I should rename it thetatf.matrix_triangular_solve
  • tf.batch_self_adjoint_eig
    • I should rename it thetatf.self_adjoint_eig
  • tf.batch_self_adjoint_eigvals
    • I should rename it thetatf.self_adjoint_eigvals
  • tf.batch_set_diag
    • I should rename it thetatf.set_diag
  • tf.batch_svd
    • I should rename it thetatf.svd
  • tf.complex_abs
    • I should rename it thetatf.abs
Other changes

Several other changes have been made, including:

  • tf.image.per_image_whitening
    • I should rename it thetatf.image.per_image_standardization
  • tf.nn.sigmoid_cross_entropy_with_logits
    • The argument to the function has been adjusted totf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=None)
  • tf.nn.softmax_cross_entropy_with_logits
    • The argument to the function has been adjusted totf.nn.softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, dim=-1, name=None)
  • tf.nn.sparse_softmax_cross_entropy_with_logits
    • The argument to the function has been adjusted totf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=None)
  • tf.ones_initializer
    • The function has been modified totf.ones_initializer()
  • tf.pack
    • I should rename it thetatf.stack
  • tf.round
    • tf.roundThe semantics of the Banker now match the rounding.
  • tf.unpack
    • I should rename it thetatf.unstack
  • tf.zeros_initializer
    • The function has been modified totf.zeros_initializer()

Reference: website