通过`library()`

时间:2019-06-18 13:22:41

标签: r package

不是每个库都装载30个包,而是可以循环执行此操作?

pckgs = c("readr", "dplyr")
sapply(pckgs, library)

背景

在加载之前,我会测试软件包是否已安装。为此,我已经以c("readr", ..., "dplyr")的形式获得了软件包名称,并且想知道我是否还可以循环加载该软件包,而不是编写30次library()

我尝试过的事情:

我简化为一个软件包:

sapply("readr", library)
sapply("readr", function(lib) library(lib))
sapply("readr", function(lib) library(get(lib)))

剧透:

我想发布此问题,然后决定检查参数中的“力字符”,感到很幸运。 (问一个问题并自己回答是有点怪异的,但是当我读到这个时,我感到很有动力:) https://stackoverflow.com/help/self-answer

1 个答案:

答案 0 :(得分:3)

参数imports *** trainloader = torch.utils.data.DataLoader( datasets.Cityscapes('/media/farshid/DataStore/temp/cityscapes/', split='train', mode='fine', target_type='semantic', target_transform =trans, transform=input_transform ), batch_size = batch_size, num_workers = 2) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") net = lolNet() criterion = CrossEntropyLoss2d() net.to(device) num_of_classes = 34 for epoch in range(int(0), 200000): lr = 0.0001 for batch, data in enumerate(trainloader, 0): inputs, labels = data labels = labels.long() inputs, labels = inputs.to(device), labels.to(device) labels = labels.view([-1, ]) optimizer = optim.Adam(net.parameters(), lr=lr) optimizer.zero_grad() outputs = net(inputs) outputs = outputs.view(-1, num_of_class) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() outputs = outputs.to('cpu') outputs = outputs.data.numpy() outputs = outputs.reshape([-1, num_of_class]) mask = np.zeros([outputs.shape[0]]) # for i in range(len(outputs)): mask[i] = np.argmax(outputs[i]) mask = mask.reshape([-1, 1]) IoU = jaccard_score(labels.to('cpu').data, mask, average='micro') 可以用于此目的。

示例:

character.only