前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow.models.rnn.rnn_cell.linear在tensorflow1.0版本之后找不到(附tensorflow1.0 API新变化)

tensorflow.models.rnn.rnn_cell.linear在tensorflow1.0版本之后找不到(附tensorflow1.0 API新变化)

作者头像
sparkexpert
发布2018-01-09 11:33:04
1.2K0
发布2018-01-09 11:33:04
举报

由于版本更新关系,从原来的tensorflow低版本到升级到tensorflow1.0以上时,发现有很多API函数变化是很正常的事情,大多碰到的如:

如其中tf.nn.rnn_cell命名空间中的很多函数都发生了命名空间的变化,如转移到了tf.contrib.rnn.core_rnn_cell。

但是在修改某个程序的时候,发现原来tensorflow.models.rnn.rnn_cell.linear这个函数,居然没有发生转移。即在tf.contrib.rnn.core_rnn_cell也没有找到。

这个暂时是无解。不过由于这个函数实现的简单的线性求和,因此可以手动在程序中进行修改。

代码语言:javascript
复制
def linear(input_, output_size, scope=None):
    '''
    Linear map: output[k] = sum_i(Matrix[k, i] * args[i] ) + Bias[k]
    Args:
        args: a tensor or a list of 2D, batch x n, Tensors.
    output_size: int, second dimension of W[i].
    scope: VariableScope for the created subgraph; defaults to "Linear".
    Returns:
    A 2D Tensor with shape [batch x output_size] equal to
    sum_i(args[i] * W[i]), where W[i]s are newly created matrices.
    Raises:
    ValueError: if some of the arguments has unspecified or wrong shape.
    '''

    shape = input_.get_shape().as_list()
    if len(shape) != 2:
        raise ValueError("Linear is expecting 2D arguments: %s" % str(shape))
    if not shape[1]:
        raise ValueError("Linear expects shape[1] of arguments: %s" % str(shape))
    input_size = shape[1]

    # Now the computation.
    with tf.variable_scope(scope or "SimpleLinear"):
        matrix = tf.get_variable("Matrix", [output_size, input_size], dtype=input_.dtype)
        bias_term = tf.get_variable("Bias", [output_size], dtype=input_.dtype)

    return tf.matmul(input_, tf.transpose(matrix)) + bias_term

上述改动只是冰山一脚,tensorflow 1.0版本的改动非常大,特别是经常碰到在函数中需要将传入参数对调顺序的那种。

现将官网上提供的新变化附置如下:(以备不时这需。这部分直接引用了http://jiqizhixin.com/article/2126文章的总结。)

API 的重要更改

  • TensorFlow/models 被移到了一个单独的 GitHub repository.
  • 除法和取模运算符(/, //, %)现已匹配 Python(flooring)语义。这也适用于 tf.div 和 tf.mod。为了获取强制的基于整数截断的行为,你可以使用 tf.truncatediv 和 tf.truncatemod.
  • tf.divide 现在是推荐的除法函数。tf.div 还将保留,但其语义将不会响应 Python 3 或 from future 机制.
  • tf.reverse 现在是将轴的索引反转。例如,tf.reverse(a, [True, False, True]) 现在必须写成 tf.reverse(a, [0, 2])。tf.reverse_v2() 暂时保留,直到 1.0 final 版.
  • tf.mul、tf.sub 和 tf.neg 被弃用,现在使用的是 tf.multiply、tf.subtract 和 tf.negative.
  • tf.pack 和 tf.unpack 被启用,现在使用的是 tf.stack 和 tf.unstack.
  • TensorArray.pack 和 TensorArray.unpack 将被启用,取而代之的是 TensorArray.stack 和 TensorArray.unstack.
  • 以下 Python 函数有参数修改,以在引用特定维度时使用 axis. 我们目前基于兼容性的考量而保留了原来的关键词参数,但我们将在 1.0 final 版中移除它们。
  • tf.argmax: dimension 变成 axis
  • tf.argmin: dimension 变成 axis
  • tf.count_nonzero: reduction_indices 变成 axis
  • tf.expand_dims: dim 变成 axis
  • tf.reduce_all: reduction_indices 变成 axis
  • tf.reduce_any: reduction_indices 变成 axis
  • tf.reduce_join: reduction_indices 变成 axis
  • tf.reduce_logsumexp: reduction_indices 变成 axis
  • tf.reduce_max: reduction_indices 变成 axis
  • tf.reduce_mean: reduction_indices 变成 axis
  • tf.reduce_min: reduction_indices 变成 axis
  • tf.reduce_prod: reduction_indices 变成 axis
  • tf.reduce_sum: reduction_indices 变成 axis
  • tf.reverse_sequence: batch_dim 变成 batch_axis, seq_dim 变成 seq_axis
  • tf.sparse_concat: concat_dim 变成 axis
  • tf.sparse_reduce_sum: reduction_axes 变成 axis
  • tf.sparse_reduce_sum_sparse: reduction_axes 变成 axis
  • tf.sparse_split: split_dim 变成 axis
  • tf.listdiff 已被重命名为 tf.setdiff1d 以匹配 NumPy 命名.
  • tf.inv 已被重命名为 tf.reciprocal(分量互逆)以避免和矩阵求逆的 np.inv 混淆
  • tf.round 现在使用了四舍六入五留双规则语义,以匹配 NumPy.
  • tf.split 现在以相反的顺序取参数,并使用了不同的关键词。特别地,我们现在将 NumPy 顺序匹配成了 tf.split(value, num_or_size_splits, axis).
  • tf.sparse_split 现在以相反的顺序取参数,并使用了不同的关键词。特别地,我们现在将 NumPy 顺序匹配成了 tf.sparse_split(sp_input, num_split, axis). 注意:现在我们暂时让 tf.sparse_split 需要关键词参数.
  • 启用 tf.concat 运算符,现在请暂时切换成 tf.concat_v2 . 在 Beta 版中,我们将更新 tf.concat 以匹配 tf.concat_v2 的参数顺序.
  • tf.image.decode_jpeg 默认使用更快的 DCT 方法. 速度的提升牺牲了一点保真度。你可以通过特定属性 dct_method='INTEGER_ACCURATE'来恢复原来的行为.
  • tf.complex_abs 已被从 Python 接口移除. 应该使用 tf.abs,它支持复数张量.
  • 模板.var_scope 属性重命名为 .variable_scope
  • SyncReplicasOptimizer 被移除,SyncReplicasOptimizerV2 重命名为 SyncReplicasOptimizer.
  • tf.zeros_initializer() 和 tf.ones_initializer() 现在返回一个 callable,其必须用 initializer 参数调用,在你的代码中用 tf.zeros_initializer() 替代 tf.zeros_initializer.
  • SparseTensor.shape 重命名为 SparseTensor.dense_shape. SparseTensorValue.shape 也一样.
  • 移除了原来的 tf summary 运算符,比如 tf.scalar_summary 和 tf.histogram_summary. 取而代之的是 tf.summary.scalar 和 tf.summary.histogram .
  • 移除 tf.train.SummaryWriter 和 tf.train.SummaryWriterCache.
  • 从公共 API 中移除 RegisterShape . 现在使用 C++ 形状函数注册.
  • 从 Python API 弃用 _ref dtypes .
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年05月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档