AttributeError: module ‘tensorflow_core.compat.v1‘ has no attribute ‘contrib‘

由于tensorflow版本问题,在tensorflow2.x版本里没有contrib组件,因此无法使用LSTMRNN实例中的sequence_loss_by_example函数。

AttributeError: module ‘tensorflow_core.compat.v1’ has no attribute ‘contrib’

解决办法:找到自己运行代码的环境下的Lib\site-packages\tensorflow_core,在我的机器上是如下:
C:\Users\Dell\anaconda3\envs\tf_gpu\Lib\site-packages\tensorflow_core
AttributeError: module ‘tensorflow_core.compat.v1‘ has no attribute ‘contrib‘_第1张图片
然后在这个文件夹下创建一个新的.py文件,即seq_loss.py文件,里面代码为
from six.moves import xrange # pylint: disable=redefined-builtin
from six.moves import zip # pylint: disable=redefined-builtin
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import embedding_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import rnn
from tensorflow.python.ops import rnn_cell_impl
from tensorflow.python.ops import variable_scope
from tensorflow.python.util import nest

def sequence_loss_by_example(logits,
targets,
weights,
average_across_timesteps=True,
softmax_loss_function=None,
name=None):
if len(targets) != len(logits) or len(weights) != len(logits):
raise ValueError("Lengths of logits, weights, and targets must be the same "
“%d, %d, %d.” % (len(logits), len(weights), len(targets)))
with ops.name_scope(name, “sequence_loss_by_example”,
logits + targets + weights):
log_perp_list = []
for logit, target, weight in zip(logits, targets, weights):
if softmax_loss_function is None:
# TODO(irving,ebrevdo): This reshape is needed because
# sequence_loss_by_example is called with scalars sometimes, which
# violates our general scalar strictness policy.
target = array_ops.reshape(target, [-1])
crossent = nn_ops.sparse_softmax_cross_entropy_with_logits(
labels=target, logits=logit)
else:
crossent = softmax_loss_function(labels=target, logits=logit)
log_perp_list.append(crossent * weight)
log_perps = math_ops.add_n(log_perp_list)
if average_across_timesteps:
total_size = math_ops.add_n(weights)
total_size += 1e-12 # Just to avoid division by 0 for all-0 weights.
log_perps /= total_size
return log_perps

这里怎么创建.py文件呢?如下步骤:
AttributeError: module ‘tensorflow_core.compat.v1‘ has no attribute ‘contrib‘_第2张图片
然后进去之后
AttributeError: module ‘tensorflow_core.compat.v1‘ has no attribute ‘contrib‘_第3张图片
然后把这个文件复制粘贴到C:\Users\Dell\anaconda3\envs\tf_gpu\Lib\site-packages\tensorflow_core下面即可。
然后再LSTMRNN实例代码添加头文件
from tensorflow_core import seq_loss
并且将计算损失调用函数语句
losses = tf.contrib.legacy_seq2seq.sequence_loss_by_example
改为
losses = seq_loss.sequence_loss_by_example
即可。
这时候运行的时候可能还有问题是
TypeError: ms_error() got an unexpected keyword argument ‘labels’
这是因为

def ms_error(self, y_target, y_pre):

return tf.square(tf.sub(y_target, y_pre))

这里定义传入参数的错误,改为
def ms_error(self,labels,logits):
return tf.square(tf.subtract(labels,logits))
应该就好了。

你可能感兴趣的:(tensorflow,深度学习,python)