欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2023/12/8 编程问答 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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

然后在这个文件夹下创建一个新的.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文件呢?如下步骤:

然后进去之后

然后把这个文件复制粘贴到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))
应该就好了。

总结

以上是生活随笔为你收集整理的AttributeError: module ‘tensorflow_core.compat.v1‘ has no attribute ‘contrib‘的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。