- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
加载 CIFAR 100 后,我尝试训练我的神经网络。但我不知道为什么会出现如下所示的越界错误
Optimizing the network with batch size 25
Epoch: 0 of 30 Average loss: -
/mnt_home/klee/LBSBGenGapSharpnessResearch/vgg.py:43: UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument.
return F.log_softmax(x)
Traceback (most recent call last):
File "plot_parametric_pytorch_cifar100.py", line 130, in <module>
loss_fn = F.nll_loss(ops, tgts)
File "/home/klee/anaconda3/envs/sharpenv/lib/python3.7/site-packages/torch/nn/functional.py", line 2115, in nll_loss
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
IndexError: Target 42 is out of bounds.
cudnn.benchmark = True
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.astype('float32')
X_train = np.transpose(X_train, axes=(0, 3, 1, 2))
X_test = X_test.astype('float32')
X_test = np.transpose(X_test, axes=(0, 3, 1, 2))
X_train /= 255
X_test /= 255
device = torch.device('cuda:0')
# This is where you can load any model of your choice.
# I stole PyTorch Vision's VGG network and modified it to work on CIFAR-10.
# You can take this line out and add any other network and the code
# should run just fine.
model = vgg.vgg11_bn()
#model.to(device)
# Forward pass
opfun = lambda X: model.forward(Variable(torch.from_numpy(X)))
# Forward pass through the network given the input
predsfun = lambda op: np.argmax(op.data.numpy(), 1)
# Do the forward pass, then compute the accuracy
accfun = lambda op, y: np.mean(np.equal(predsfun(op), y.squeeze()))*100
# Initial point
x0 = deepcopy(model.state_dict())
# Number of epochs to train for
# Choose a large value since LB training needs higher values
# Changed from 150 to 30
nb_epochs = 30
batch_range = [25, 40, 50, 64, 80, 128, 256, 512, 625, 1024, 1250, 1750, 2048, 2500, 3125, 4096, 4500, 5000]
# parametric plot (i.e., don't train the network)
hotstart = False
if not hotstart:
for batch_size in batch_range:
optimizer = torch.optim.Adam(model.parameters())
model.load_state_dict(x0)
#model.to(device)
average_loss_over_epoch = '-'
print('Optimizing the network with batch size %d' % batch_size)
np.random.seed(1337) #So that both networks see same sequence of batches
for e in range(nb_epochs):
model.eval()
print('Epoch:', e, ' of ', nb_epochs, 'Average loss:', average_loss_over_epoch)
average_loss_over_epoch = 0
# Checkpoint the model every epoch
torch.save(model.state_dict(), "./models/30EpochCIFAR100ExperimentBatchSize" + str(batch_size) + ".pth")
array = np.random.permutation(range(X_train.shape[0]))
slices = X_train.shape[0] // batch_size
beginning = 0
end = 1
# Training loop!
for _ in range(slices):
start_index = batch_size * beginning
end_index = batch_size * end
smpl = array[start_index:end_index]
model.train()
optimizer.zero_grad()
ops = opfun(X_train[smpl])
tgts = Variable(torch.from_numpy(y_train[smpl]).long().squeeze())
loss_fn = F.nll_loss(ops, tgts) <--- errorring linne
average_loss_over_epoch += loss_fn.data.numpy() / (X_train.shape[0] // batch_size)
loss_fn.backward()
optimizer.step()
beginning += 1
end += 1
__all__ = [
'VGG', 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn',
'vgg19_bn', 'vgg19',
]
model_urls = {
'vgg11': 'https://s3.amazonaws.com/pytorch/models/vgg11-fb7e83b2.pth',
'vgg13': 'https://s3.amazonaws.com/pytorch/models/vgg13-58758d87.pth',
'vgg16': 'https://s3.amazonaws.com/pytorch/models/vgg16-82412952.pth',
'vgg19': 'https://s3.amazonaws.com/pytorch/models/vgg19-341d7465.pth',
}
class VGG(nn.Module):
def __init__(self, features):
super(VGG, self).__init__()
self.features = features
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(512, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Linear(4096, 10),
)
self._initialize_weights()
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return F.log_softmax(x)
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
n = m.weight.size(1)
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
def make_layers(cfg, batch_norm=False):
layers = []
in_channels = 3
for v in cfg:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
if batch_norm:
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
else:
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
return nn.Sequential(*layers)
cfg = {
'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
def vgg11(pretrained=False, **kwargs):
"""VGG 11-layer model (configuration "A")
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = VGG(make_layers(cfg['A']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg11']))
return model
def vgg11_bn(**kwargs):
"""VGG 11-layer model (configuration "A") with batch normalization"""
return VGG(make_layers(cfg['A'], batch_norm=True), **kwargs)
最佳答案
您正在使用 CIFAR-100,它有 100 个类(因此得名)。但是您的模型只能预测 10 个类别。自然,任何超过 10 的类都会导致错误。
模型分类器中最后一个线性线性的输出需要更改为 100:
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(512, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Linear(4096, 100),
)
关于python - ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index) 抛出 IndexError : Target 42 is out of bounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61968540/
我有一个 div,我希望使用 css 显示为 :target。到目前为止,这工作正常。我的问题是:我希望它淡入淡出。 我的代码: Content #stuff { opacity:0;
我想找到在Rust构建中使用的libc.so文件,以便我可以使用--version进行查询。 (一些libcs通过C宏公开其版本信息,因此,它们的另一种选择是在构建脚本中使用cc条板箱。但其他诸如
我仍然不明白为什么 Makefile 中的“虚假”规则将“.PHONY”作为其目标。作为先决条件,这会更合乎逻辑。 我需要详细说明这一点吗?如果A依赖于B并且B是假的,那么A也是假的。因此,与 .PH
在 Fortran 语言中,向具有 TARGET 属性的虚拟参数的过程提供不具有 TARGET 属性的参数应该会导致无效代码。但是,当使用 gfortran (5.1.0) 或 ifort (14.0
如果有人发布过相同的问题,请原谅我,我找不到类似问题的答案。 当我单击带有 #dotNetComponents url 的按钮时,它会将我带到带有 dotNetComponents ID 的 div,
我想用 :target 伪类更改我的 html 中元素的样式。我的标记(第一个是按钮,第二个是目标元素): Call to action target CSS: #btn01:target { b
下面提到的示例代码是 Keith Wood 的 jQuery Countdown 插件的一部分。有人能解释一下吗 _attachCountdown: function(target, options)
我是 React 的新手。这绝对让我感到困惑。我可以使用 event.target 访问 HTML 元素,它显示的值等于某个数字,但每次我使用 event.target.value 时,我都会得到 u
这个问题是关于交叉编译的。 使用 swift 编译器的 -target 或 -target-cpu 选项可以使用哪些不同的目标?我在哪里可以找到概述? 它只能用于创建 iOS/watchOS 应用程序
在CKEditor 5中,我没有在链接对话框中看到目标属性的字段。 如何添加这样的字段?或将target = _blank设置为默认值。 谢谢 最佳答案 从Link Plugin的11.1.0版本开始
问题:FAKE 中是否有一个命令可以打印构建脚本中所有定义的目标? 我想以这样的方式设置我的 FAKE 构建:当我不指定目标时,它会打印构建脚本中所有可用目标的列表。 例如: > build.cmd
尝试使用 Visual Studio 2013 Update 3 创建一个新的 Cordova“空白应用程序”。 我看到了模板,但是当尝试打开空白应用程序时,我得到: The imported pro
http://download.oracle.com/javase/6/docs/api/java/lang/annotation/Target.html 此元注释指示声明的类型仅用作复杂注释类型声明
使用CocoaPods,有什么区别 target :TargetName do # Some pods... end 和 target "TargetName" do #
我正在尝试仅使用 CSS 制作一个简单的移动菜单切换。通过显示和隐藏两个按钮,这些按钮具有指向显示或隐藏导航菜单的类的不同链接。 是本教程的编辑link ,但现在我想让关闭和打开按钮位于单独的 div
以下是包含简单日志文件目标的简单 nlog 配置。我的问题是如何为 Nlog.Targets.Redis 添加目标? 最佳答案 以下是 NLog.Targets.Redis 的正确配置。如果
我想知道您是否可以将一个单元测试包链接到多个目标。因此,可以使用一个测试包测试所有应用程序目标。 我在所有应用程序目标之间有一些共享代码,但也有一些基于正在运行的应用程序目标的特定计算。 目前,如果我
我在 VSTS 中使用部署组将我的应用程序部署到本地测试 Web 服务器。 它已经运行良好很长时间了,但是在大约 6 周没有使用它之后,我现在遇到了这个错误,我想修复它; 最佳答案 您的代理未运行或无
使用 CMake 构建开源项目时(在我的例子中,它是柠檬图库),当我尝试通过 -DBUILD_SHARED_LIBS=1 构建共享库时出现此错误。 : TARGETS given no LIBRARY
尝试安装 ionic,添加 android 平台时出现以下错误 Error: Please install Android target "android-19". Hint: Run "androi
我是一名优秀的程序员,十分优秀!