日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2023/12/8 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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‘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。