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

相关推荐
Think_Higher9 分钟前
CID行业趋势周报|7.27—8.02:大盘表现、重点赛道与投测建议
大数据·人工智能
MC皮蛋侠客14 分钟前
SQLAlchemy 系列(八):AsyncIO、并发与 Web 生命周期——让每个并发任务持有自己的 Session
数据库·python
林澈在路上34 分钟前
2026 主流 AI 写歌 APP 全面测评|零基础原创歌曲工具选购指南
大数据·人工智能·aigc·音视频·音频
lancyu35 分钟前
零基础AI应用编程开发入门 | 吴恩达Prompt工程极简通关指南:新手从零学会工业级提示词开发(可直接复用代码)
人工智能·深度学习·机器学习
科研小刘带你玩学术37 分钟前
AI Agent正在改变人工智能应用模式:从工具助手走向自主智能系统
人工智能·大语言模型·未来趋势·智能体·智能系统
阿文和她的Key39 分钟前
一个失控的AI智能体在4.5天内完成了17,600次攻击:从这次事件看AI安全治理的四层裂痕
人工智能·安全
东方小月1 小时前
从零开发一个 Coding Agent(五):使用 TypeBox 校验工具参数
前端·人工智能·后端
做前端的娜娜子1 小时前
TRAE-Work-周报自动化实战
人工智能·设计
LaughingZhu1 小时前
Product Hunt 每日热榜 | 2026-08-03
人工智能·经验分享·深度学习·神经网络·产品运营
大龄码农有梦想1 小时前
AI Agent 目前最大的瓶颈是什么?
人工智能·机器学习·ai agent·智能体·ai工作流·智能体平台