注意使用 nn.CrossEntropyLoss()作为损失函数,最后一层输出不要softmax了,因为nn.CrossEntropyLoss()已经包含了该操作

# Build a feed-forward network


model = nn.Sequential(nn.Linear(784, 128),
                      nn.ReLU(),
                      nn.Linear(128, 64),
                      nn.ReLU(),
                      nn.Linear(64, 10))

# Define the loss
criterion = nn.CrossEntropyLoss()

# Get our data
images, labels = next(iter(trainloader))
# Flatten images
images = images.view(images.shape[0], -1)

# Forward pass, get our logits
logits = model(images)
# Calculate the loss with the logits and the labels
loss = criterion(logits, labels)

print(loss)

 

使用 nn.LogSoftmax 或 F.log_softmax文档)构建具有 log-softmax 输出的模型更方便。然后我们可以通过计算指数 torch.exp(output) 获得实际概率。对于 log-softmax 输出,你需要使用负对数似然损失 nn.NLLLoss文档)。

练习:请构建一个返回 log-softmax 输出结果并使用负对数似然损失计算损失的模型。注意,对于 nn.LogSoftmax 和 F.log_softmax,你需要相应地设置 dim 关键字参数。dim=0 会计算各行的 softmax,使每列的和为 1,而 dim=1 会计算各列的 softmax,使每行的和为 1。思考下你希望输出是什么,并选择恰当的 dim

https://classroom.udacity.com/nanodegrees/nd009-cn-advanced/parts/5f4d630c-d15a-412c-aaeb-b57ad61cd03c/modules/3aa9e812-62cd-4ae3-8fc4-593538f08455/lessons/9b014a97-2267-4f1b-af97-284b7dac2a58/concepts/572cd59e-540f-43d9-906f-33d22a4452a6

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值