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

相关推荐
Narutolxy1 小时前
大模型数据分析破局之路20250512
人工智能·chatgpt·数据分析
浊酒南街1 小时前
TensorFlow中数据集的创建
人工智能·tensorflow
码上淘金2 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo2 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
2301_787552873 小时前
console-chat-gpt开源程序是用于 AI Chat API 的 Python CLI
人工智能·python·gpt·开源·自动化
懵逼的小黑子3 小时前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django
layneyao3 小时前
AI与自然语言处理(NLP):从BERT到GPT的演进
人工智能·自然语言处理·bert
Y3174293 小时前
Python Day23 学习
python·学习
Ai尚研修-贾莲4 小时前
Python语言在地球科学交叉领域中的应用——从数据可视化到常见数据分析方法的使用【实例操作】
python·信息可视化·数据分析·地球科学
jndingxin4 小时前
OpenCV 的 CUDA 模块中用于将多个单通道的 GpuMat 图像合并成一个多通道的图像 函数cv::cuda::merge
人工智能·opencv·计算机视觉