with an unsupported type (<class ‘method‘>) to a Tensor.

原因:tensorflow 和numpy 版本不兼容

解决方法:版本更换

但是我懒

另一个解决方法:将numpy转换为array,运行tf,convert_to_tensor(array)

在学习深度学习时在单独的tensorflow 下面进行,numpy tensorflow版本如下

print(tf.__version__)
print(np.__version__)

#运行结果
2.3.0
1.23.5

开始的运行如下

train_x=pd.read_csv(Loc_dress+"Input_water_train.csv",header=None)
print(train_x.head(5))
train_X=train_x.to_numpy
train_X=tf.convert_to_tensor(train_X,dtype=tf.float32)

运行相继报错:

with an unsupported type (<class 'method'>) to a Tensor.

搜索相关问题回答,应该是tensorflow 与numpy版本不兼容导致,但是又不想去重新调版本

后来发现当变量类型为array 时,使用tf.convert_to_tensor(array)不会报错,所以将其转换为dataframe直接转换为array,运行成功!!

train_x=pd.read_csv(Loc_dress+"Input_water_train.csv",header=None)
print(train_x.head(5))
print(type(train_x))
train_X=np.array(train_x)
train_X=tf.convert_to_tensor(train_X,dtype=tf.float32)

### GIoU 边界回归损失的概念 Generalized Intersection over Union (GIoU) 是一种改进的边界框回归损失函数,旨在解决传统 IoU 损失在某些情况下的局限性。具体来说,当预测框和真实框完全没有重叠时,传统的 IoU 损失无法提供有效的梯度信息来指导模型优化[^2]。 GIoU 的定义如下: \[ \text{GIoU} = \text{IoU} - \frac{\text{C} - \text{union}}{\text{C}} \] 其中: - \( C \) 表示包含两个边界框最小闭包区域; - \( \text{union} \) 表示两个边界框的并集面积; - \( \text{IoU} \) 则表示交并比。 通过引入这一额外项,即使预测框与真实框不相交,也能给出有意义的方向性惩罚,从而更好地引导网络学习更精确的位置调整。 ### 实现代码示例 以下是基于 PyTorch 实现的一个简单版本的 GIoU 损失函数: ```python import torch from typing import Tuple, Optional def box_area(boxes: torch.Tensor) -> torch.Tensor: """ 计算给定边界的面积. 参数: boxes (Tensor[N, 4]): 边界框坐标格式为[xmin, ymin, xmax, ymax]. 返回: Tensor[N]: 各个边界的面积. """ return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) def generalized_box_iou( boxes1: torch.Tensor, boxes2: torch.Tensor, ) -> torch.Tensor: """ 计算两组边界之间的广义IOU. 参数: boxes1 (Tensor[N, 4]) boxes2 (Tensor[M, 4]) 返回: iou (Tensor[N, M]): N×M矩阵形式的结果. """ area1 = box_area(boxes1) area2 = box_area(boxes2) lt = torch.max(boxes1[:, None, :2], boxes2[:, :2]) # [N,M,2] rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:]) # [N,M,2] wh = (rb - lt).clamp(min=0) # [N,M,2] inter = wh[:, :, 0] * wh[:, :, 1] # [N,M] union = area1[:, None] + area2 - inter iou = inter / union lti = torch.min(boxes1[:, None, :2], boxes2[:, :2]) rbi = torch.max(boxes1[:, None, 2:], boxes2[:, 2:]) whi = (rbi - lti).clamp(min=0) areai = whi[:, :, 0] * whi[:, :, 1] giou = iou - ((areai - union)/areai) return giou class GeneralizedBoxIOLoss(torch.nn.Module): def __init__(self, reduction='mean'): super().__init__() self.reduction = reduction def forward(self, input_boxes: torch.Tensor, target_boxes: torch.Tensor) -> torch.Tensor: loss = 1.0 - torch.diag(generalized_box_iou(input_boxes, target_boxes)) if self.reduction == 'mean': return loss.mean() elif self.reduction == 'sum': return loss.sum() else: raise ValueError(f'Unsupported reduction method {self.reduction}') ``` 此实现提供了 `generalized_box_iou` 函数用于计算任意数量对之间的一般化交并比,并封装了一个模块类 `GeneralizedBoxIOLoss` 来方便地应用于训练过程中作为损失函数的一部分.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值