用pytorch实现两个矩阵之间的距离运算

本文介绍如何使用PyTorch计算两个向量之间每个点的距离,并根据阈值筛选出超出特定值的点,最后计算平均值。在实现过程中,遇到了索引超出范围的错误,详细分析了错误原因并提供了完整的解决方案代码。
摘要由CSDN通过智能技术生成

说明:
a向量有m个点,b向量有n个点,计算a中每一个点与b中所有点的距离,并用阈值筛选出大于某个值的个数,求平均值

debug
用pytorch进行矩阵运算时出现如下错误,原因是索引超出范围

 lymph_num += len(dist[dist < 500])#代码行
RuntimeError: CUDA error: device-side assert triggered
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

原因是因为索引超出范围

完整代码

import pandas as pd
import numpy as np
import os
from itertools import chain
import h5py
from tqdm import tqdm
from glob import glob
from scipy.io import loadmat
import torch.nn.functional as F
import torch
Mat_path = '/media/hero/fengzy/TO_FeiHu_MAT'
out_put  = './out_put'
h5_path = '/home/hero/disk/ubuntu_free/Hou_feihu/out_put/h5_file'

def save_hdf5(output_path, asset_dict, attr_dict= None, mode='a'):

    file = h5py.File(output_path, mode)

    for key, val in asset_dict.items():

        # data_shape = val.shape
        try:
            data_shape = val.shape
        except:
            import pdb
            pdb.set_trace()


        if key not in file:
            data_type = val.dtype
            chunk_shape = (1, ) + data_shape[1:]
            maxshape = (None, ) + data_shape[1:]
            dset = file.c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值