文档反馈

问题文档片段

问题文档片段包含公式时,显示为空格。

提交类型
issue

有点复杂...

找人问问吧。

PR

小问题,全程线上修改...

一键搞定!

请选择提交类型

问题类型
规范和低错类

- 规范和低错类:

- 错别字或拼写错误,标点符号使用错误、公式错误或显示异常。

- 链接错误、空单元格、格式错误。

- 英文中包含中文字符。

- 界面和描述不一致,但不影响操作。

- 表述不通顺,但不影响理解。

- 版本号不匹配:如软件包名称、界面版本号。

易用性

- 易用性:

- 关键步骤错误或缺失,无法指导用户完成任务。

- 缺少主要功能描述、关键词解释、必要前提条件、注意事项等。

- 描述内容存在歧义指代不明、上下文矛盾。

- 逻辑不清晰,该分类、分项、分步骤的没有给出。

正确性

- 正确性:

- 技术原理、功能、支持平台、参数类型、异常报错等描述和软件实现不一致。

- 原理图、架构图等存在错误。

- 命令、命令参数等错误。

- 代码片段错误。

- 命令无法完成对应功能。

- 界面错误,无法指导操作。

- 代码样例运行报错、运行结果不符。

风险提示

- 风险提示:

- 对重要数据或系统存在风险的操作,缺少安全提示。

内容合规

- 内容合规:

- 违反法律法规,涉及政治、领土主权等敏感词。

- 内容侵权。

请选择问题类型

问题描述

点击输入详细问题描述,以帮助我们快速定位问题。

mindspore.ops.EditDistance

查看源文件
class mindspore.ops.EditDistance(normalize=True)[源代码]

计算Levenshtein编辑距离。它用于测量两个序列的相似性。输入是可变长度的序列,由SpaseTensors(hypothesis_indices, hypothesis_values, hypothesis_shape)和(truth_indices, truth_values, truth_shape)提供。

leva,b(i,j)={max(i,j)  if min(i,j)=0min{leva,b(i1,j)+1leva,b(i,j1)+1 otherwise. leva,b(i1,j1)+1(aibj)

其中 a 表示预测值, b 表示真实值。为了便于理解,这里的i和j可以被视为a和b的长度。

警告

  • 如果输入 truth_indices 或者 hypothesis_indices 不是有序的,可能会导致计算结果不符合预期,建议调用该接口之前确保输入的稀疏张量 truth_indiceshypothesis_indices 都是升序排列的。

参数:
  • normalize (bool) - 如果为 True ,则编辑距离将按真实值长度标准化。默认值: True

输入:
  • hypothesis_indices (Tensor) - 预测列表的索引。类型为Tensor,数据类型为int64,其shape为 (N,R)

  • hypothesis_values (Tensor) - 预测列表的值。类型为Tensor,必须是长度为N的一维向量。

  • hypothesis_shape (Tensor) - 预测列表的shape。类型为Tensor,必须是长度为R的向量,数据类型为int64。只能是常量。

  • truth_indices (Tensor) - 真实列表的索引。类型为Tensor,数据类型为int64,其shape为 (M,R)

  • truth_values (Tensor) - 真实列表的值。类型为Tensor,必须是长度为M的一维向量。

  • truth_shape (Tensor) - 真实列表的shape。类型为Tensor,必须是长度为R的向量,数据类型为int64。只能是常量。

输出:

Tensor,其秩为 R-1 ,数据类型为float32。

异常:
  • TypeError - 如果 normalize 不是bool。

支持平台:

Ascend CPU

样例:

>>> import numpy as np
>>> from mindspore import Tensor
>>> import mindspore.nn as nn
>>> from mindspore import ops
>>> class EditDistance(nn.Cell):
...     def __init__(self, hypothesis_shape, truth_shape, normalize=True):
...         super(EditDistance, self).__init__()
...         self.edit_distance = ops.EditDistance(normalize)
...         self.hypothesis_shape = hypothesis_shape
...         self.truth_shape = truth_shape
...
...     def construct(self, hypothesis_indices, hypothesis_values, truth_indices, truth_values):
...         return self.edit_distance(hypothesis_indices, hypothesis_values, self.hypothesis_shape,
...                                   truth_indices, truth_values, self.truth_shape)
...
>>> hypothesis_indices = Tensor(np.array([[0, 0, 0], [1, 0, 1], [1, 1, 1]]).astype(np.int64))
>>> hypothesis_values = Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> hypothesis_shape = Tensor(np.array([1, 1, 2]).astype(np.int64))
>>> truth_indices = Tensor(np.array([[0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1]]).astype(np.int64))
>>> truth_values = Tensor(np.array([1, 3, 2, 1]).astype(np.float32))
>>> truth_shape = Tensor(np.array([2, 2, 2]).astype(np.int64))
>>> edit_distance = EditDistance(hypothesis_shape, truth_shape)
>>> output = edit_distance(hypothesis_indices, hypothesis_values, truth_indices, truth_values)
>>> print(output)
[[1. 1.]
 [1. 1.]]