deeplearning with pytorch (五)

.view()方法在PyTorch中用于重塑张量。这里它被用来将单个样本的张量重塑成模型所期望的输入形状。具体地,1,1,28,28意味着创建一个新的张量,其中:

  • 第一个1代表批次大小(batch size),这里为1,因为你只预测一个样本。
  • 第二个1可能代表颜色通道的数量,这在处理灰度图像时常见,意味着每个像素只有一个颜色值。对于RGB图像,这个数字会是3。
  • 28,28代表图像的高度和宽度,这是典型的MNIST手写数字数据集的维度。
python 复制代码
#graph the loss at epoch
train_losses = [tl.item() for tl in train_losses]
plt.plot(train_losses, label= "training loss")
plt.plot(test_losses, label="validation loss")
plt.title("loss at epoch")
plt.legend()
#graph the accuracy at the end of each epoch
plt.plot([t/600 for t in train_correct], label = "training accuracy")
plt.plot([t/100 for t in test_correct], label = "validation accuracy")
plt.title("accuracy at the end of each epoch")
plt.legend()
test_load_everything = DataLoader(test_data, batch_size= 10000, shuffle= False)
with torch.no_grad():
    correct = 0
    for X_test, y_test in test_load_everything:
        y_val = model(X_test)
        predicted = torch.max(y_val, 1)[1]
        correct += (predicted == y_test).sum()

# did for correct 
correct.item()/len(test_data) * 100
## Send New Image Thru The Model
# grab an image
test_data[4143] #tensor with an image in it ... at end ,it shows the label
# grab just the data 
test_data[4143][0]
#reshape it 
test_data[4143][0].reshape(28,28)
# show the image 

plt.imshow(test_data[4143][0].reshape(28,28))
# pass the image thru our model
model.eval()
with torch.no_grad():
    new_prediction = model(test_data[4143][0].view(1,1,28,28)) #batch size of 1,1 color channel, 28x28 image
    
# check the new prediction, get probabilities
new_prediction
new_prediction.argmax()

完整的py文件见GitHub - daichang01/neraual_network_learning at dev

相关推荐
XR101yqm12211 小时前
Maya 2027.3深度解读:AI直改场景+视口提速60%,工作室管线又要升级了?
人工智能·云渲染·maya
Qyr991 小时前
2026-2032全球锆靶行业深度分析:技术路径、竞争格局与市场机遇
大数据·人工智能·物联网
天远API1 小时前
零信任架构实战:基于天远车辆过户详版查询构建自动化二手车残值估值网关
运维·人工智能·架构·自动化
期货资管源码1 小时前
内盘期货中远期与跨品种套利开发笔记:价差、配对与执行细节
笔记·python·量化交易·期货·套利
2601_958352902 小时前
I2S转模拟音频,真的只要5个引脚+1个电容?
前端·人工智能·单片机·嵌入式硬件
AI大模型-小雄2 小时前
Codex 如何自动修复项目 Bug?从错误定位到测试验证的实战教程
python·程序员·bug·ai编程·codex·chatgpt plus·chatgpt pro
极客编程玩家2 小时前
SpringAI RAG 检索增强完整实战案例
人工智能·ai·aigc·ai编程
LaughingZhu2 小时前
Product Hunt 热榜 2026-12-05:SoduWork、finerd split、Otari by Mozilla AI
java·人工智能·docker·chatgpt·容器
风月说与山鬼3 小时前
五、python基础——集合、映射类型
python
KingOTC3 小时前
FDE的组织方式:团队怎么搭、仗怎么打
人工智能·fde