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

相关推荐
艾克斯观察5 分钟前
一条辞职帖,14小时7700万人看:全网到底在吵什么
人工智能·ai·x·世界末日·传播学
TMT星球9 分钟前
松延动力推出具身智能子品牌Scalabot:让智能,在真实世界生长
大数据·人工智能
hhb_61810 分钟前
智能任务自动化协同AI工作流技术文档
运维·人工智能·自动化
YOLO数据集集合13 分钟前
福寿螺目标检测数据集 | 福寿螺检测 入侵物种 农业植保 目标检测9062期
人工智能·yolo·目标检测·计算机视觉·福寿螺·福寿螺检测·入侵物种
深度学习lover14 分钟前
<数据集>电力劳保穿戴识别<目标检测>
人工智能·yolo·目标检测·计算机视觉·数据集·电力劳保穿戴识别
恒知学术15 分钟前
写文献综述前,先找综述论文,再用摘要筛掉不相关文献
人工智能·深度学习·文献综述·文献检索·研究方法·论文辅导·申博辅导
liliangcsdn16 分钟前
IC计算-前向/远期收益计算和代码示例
开发语言·python·pandas
绿智校园26 分钟前
拉孚在AI领域的应用全景:从DeepBasic Folar数据底座到空间认知智能体,如何让物理空间“长“出智能
人工智能
2601_9623818627 分钟前
python安装好了如何设置环境变量
python·开发工具·环境变量·虚拟环境·ide配置
美团技术团队28 分钟前
《Agent 评测白皮书》系列01:Agent 评测全览
人工智能