sequence_loss_by_example()函数在Tensorflow2.x找不到
生活随笔
收集整理的這篇文章主要介紹了
sequence_loss_by_example()函数在Tensorflow2.x找不到
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
sequence_loss_by_example()函數(shù)在tensorflow2.0以上版本無法直接使用,原本該函數(shù)保存在contrib模塊中,如果想要直接使用該函數(shù)又不想將tensorflow降至1.x版本,可以將sequence_loss_by_example()函數(shù)直接寫入pycharm中,在開始import該.py文件即可。
1.在pycharm中新建seq_loss.py文件
2.寫入代碼
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 nestdef 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_sizereturn log_perps3.在要使用sequence_loss_by_example()函數(shù)的模塊中頭文件import?seq_loss,在代碼處使用
????????losses =seq_loss.sequence_loss_by_example()總結(jié)
以上是生活随笔為你收集整理的sequence_loss_by_example()函数在Tensorflow2.x找不到的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鲁班H5页面生成工具
- 下一篇: Chinaren校友录所用的左边弹出式菜