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

相关推荐
梦丶晓羽2 分钟前
自然语言处理:最大期望值算法
人工智能·python·自然语言处理·高斯混合模型·最大期望值算法
gis收藏家4 分钟前
使用开放数据、ArcGIS 和 Sklearn 测量洛杉矶的城市相似性
人工智能·arcgis·sklearn
君科程序定做2 小时前
PDFMathTranslate安装使用
python
helpme流水2 小时前
【人工智能】Open WebUI+ollama+deepSeek-r1 本地部署大模型与知识库
人工智能·ubuntu·ai
Linzerox3 小时前
Pycharm 取消拼写错误检查(Typo:in word xxx)
python·pycharm
千里码aicood3 小时前
[含文档+PPT+源码等]精品基于Python实现的校园小助手小程序的设计与实现
开发语言·前端·python
Icomi_4 小时前
【神经网络】0.深度学习基础:解锁深度学习,重塑未来的智能新引擎
c语言·c++·人工智能·python·深度学习·神经网络
半问4 小时前
广告营销,会被AI重构吗?
人工智能·重构
蠟筆小新工程師4 小时前
Deepseek可以通过多种方式帮助CAD加速工作
开发语言·python·seepdeek
movee4 小时前
一台低配云主机也能轻松愉快地玩RDMA
linux·人工智能·后端