我有一个模型,尝试在 DDP 模式下与训练器一起使用。
import pytorch_lightning as pl
import torch
import torchvision
from torchmetrics import Accuracy
class Model(pl.LightningModule):
def __init__(
self,
model_name: str,
num_classes: int,
model_hparams: Dict["str", Union[str, int]],
optimizer_name: str,
optimizer_hparams: Dict["str", Union[str, int]],
):
super().__init__()
self.save_hyperparameters()
self.model = torchvision.resnet18(num_classes=num_classes, **model_hparams)
self.loss_module = CrossEntropyLoss()
self.example_input_array = torch.zeros((1, 3, 512, 512), dtype=torch.float32)
# Trying to use in DDP mode
self.test_accuracy = Accuracy(num_classes=num_classes)
def forward(self, imgs) -> Tensor:
return self.model(imgs)
# <redacted training_*, val_*, etc. as they are not relevant>
def test_step(self, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试替换正则表达式找到的匹配项的某个部分。相关字符串具有以下格式:
"<Random text>[Text1;Text2;....;TextN]<Random text>"
Run Code Online (Sandbox Code Playgroud)
所以基本上可以有 N 个用“;”分隔的文本 括号内。我的目标是改变“;” 变成“,”(但仅适用于这种格式的字符串),以便我可以保留“;” 作为 CSV 文件的分隔符。所以结果应该是:
"<Random text>[Text1,Text2,...,TextN]<Random text>"
Run Code Online (Sandbox Code Playgroud)
我可以用类似的东西匹配相关的字符串
re.compile(r'\[".*?((;).*?){1,4}"\]')
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试使用 sub 方法,它会替换整个字符串。
我已经搜索过 stackoverflow,我很确定“捕获组”可能是解决方案,但我并没有真正到达那里。谁能帮我?
我只想改变“;” 在我的文本文件的 ["Text1;...;TextN"] 部分。
我正在评估 pytorch 模型。它以以下方式给出结果
results = model(batch)
# results is a list of dictionaries with 'boxes', 'labels' and 'scores' keys and torch tensor values
Run Code Online (Sandbox Code Playgroud)
然后我尝试打印一些值来检查发生了什么
print(
(
f"{results[0]['boxes'].shape[0]}\n" # Returns how many boxes there is
f"{results[0]['scores'].mean()}" # Mean credibility score of the boxes
)
)
Run Code Online (Sandbox Code Playgroud)
这会导致错误
Exception has occurred: RuntimeError: operation does not have identity
让事情变得更加混乱的是,print
有时只会失败。为什么会失败?