当前位置: 首页>>代码示例>>Python>>正文


Python Tensor.numpy方法代码示例

本文整理汇总了Python中torch.Tensor.numpy方法的典型用法代码示例。如果您正苦于以下问题:Python Tensor.numpy方法的具体用法?Python Tensor.numpy怎么用?Python Tensor.numpy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在torch.Tensor的用法示例。


在下文中一共展示了Tensor.numpy方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_predicted_clusters

# 需要导入模块: from torch import Tensor [as 别名]
# 或者: from torch.Tensor import numpy [as 别名]
    def get_predicted_clusters(top_spans: torch.Tensor,
                               antecedent_indices: torch.Tensor,
                               predicted_antecedents: torch.Tensor) -> Tuple[List[Tuple[Tuple[int, int], ...]],
                                                                             Dict[Tuple[int, int],
                                                                                  Tuple[Tuple[int, int], ...]]]:
        # Pytorch 0.4 introduced scalar tensors, so our calls to tuple() and such below don't
        # actually give ints unless we convert to numpy first.  So we do that here.
        top_spans = top_spans.numpy()  # (num_spans, 2)
        antecedent_indices = antecedent_indices.numpy()  # (num_spans, num_antecedents)
        predicted_antecedents = predicted_antecedents.numpy()  # (num_spans,)

        predicted_clusters_to_ids: Dict[Tuple[int, int], int] = {}
        clusters: List[List[Tuple[int, int]]] = []
        for i, predicted_antecedent in enumerate(predicted_antecedents):
            if predicted_antecedent < 0:
                continue

            # Find predicted index in the antecedent spans.
            predicted_index = antecedent_indices[i, predicted_antecedent]
            # Must be a previous span.
            assert i > predicted_index
            antecedent_span: Tuple[int, int] = tuple(top_spans[predicted_index])  # type: ignore

            # Check if we've seen the span before.
            if antecedent_span in predicted_clusters_to_ids.keys():
                predicted_cluster_id: int = predicted_clusters_to_ids[antecedent_span]
            else:
                # We start a new cluster.
                predicted_cluster_id = len(clusters)
                clusters.append([antecedent_span])
                predicted_clusters_to_ids[antecedent_span] = predicted_cluster_id

            mention: Tuple[int, int] = tuple(top_spans[i])  # type: ignore
            clusters[predicted_cluster_id].append(mention)
            predicted_clusters_to_ids[mention] = predicted_cluster_id

        # finalise the spans and clusters.
        final_clusters = [tuple(cluster) for cluster in clusters]
        # Return a mapping of each mention to the cluster containing it.
        mention_to_cluster: Dict[Tuple[int, int], Tuple[Tuple[int, int], ...]] = {
                mention: final_clusters[cluster_id]
                for mention, cluster_id in predicted_clusters_to_ids.items()
                }

        return final_clusters, mention_to_cluster
开发者ID:apmoore1,项目名称:allennlp,代码行数:47,代码来源:conll_coref_scores.py


注:本文中的torch.Tensor.numpy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。