msg = ex.args[0] elf.assertRegex(msg, )
时间: 2024-05-31 13:08:08 浏览: 12
As an AI language model, I cannot fill in the blank without additional context. Please provide more information about the specific error message and what regular expression you are trying to assert.
相关问题
def test_most_built_in_classes_cannot_be_monkey_patched(self): try: int.is_even = lambda self: (self % 2) == 0 except Exception as ex: err_msg = ex.args[0] self.assertRegex(err_msg, "can't set attributes of built-in")
这个修改后的测试用例中,`int.is_even` 属性被定义为一个 lambda 函数,用于判断一个整数是否为偶数。由于 `int` 类型是不可变类型,因此对其属性进行修改时会抛出一个异常,这个异常信息被保存在 `err_msg` 变量中。然后通过 `self.assertRegex()` 方法来判断异常信息中是否包含 "can't set attributes of built-in" 字符串,如果包含则测试通过,否则测试失败。
然而,由于内置类型是不允许被猴子补丁的,因此这个测试用例无法通过。如果你想要测试猴子补丁,可以将 `int` 类型改为自己定义的一个类,然后再进行猴子补丁。具体可以参考我上一个回答中的示例代码。
将这段代码改为分类损失if self.args.pnorm==1: dis = F.relu(norm(entity_embs - concept_embs,pnorm=self.args.pnorm) - torch.abs(radius)) else: dis = F.relu(norm(entity_embs - concept_embs,pnorm=self.args.pnorm) - radius ** 2) loss = F.relu(dis[0] + self.args.margin_ins - dis[1]).sum()
以下是修改后的代码:
```python
if self.args.pnorm == 1:
dis = F.relu(norm(entity_embs - concept_embs, pnorm=self.args.pnorm) - torch.abs(radius))
else:
dis = F.relu(norm(entity_embs - concept_embs, pnorm=self.args.pnorm) - radius ** 2)
pos_dis = dis[0]
neg_dis = dis[1:]
pos_loss = F.relu(self.args.margin_ins - pos_dis)
neg_loss = F.relu(neg_dis - self.args.margin_outs)
loss = pos_loss.sum() + neg_loss.sum()
```
这里将原来的对比损失改为了分类损失,将正样本和负样本分别计算距离和损失。其中,正样本只计算与半径之间的距离,用于判断是否将实体嵌入到概念空间中;负样本计算与半径之间的距离和与正样本之间的距离,用于判断是否将实体从概念空间中排除出去。最终的损失是正样本损失和负样本损失之和。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)