【卷积神经网络】例题XO - Python实现 - PyTorch版本
2022/5/4 1:43:26
本文主要是介绍【卷积神经网络】例题XO - Python实现 - PyTorch版本,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目及理论讲解:
【卷积神经网络】例题XO - Python实现 - HBU_DAVID - 博客园 (cnblogs.com)
PyTorch实现代码:
# https://blog.csdn.net/qq_26369907/article/details/88366147 # https://zhuanlan.zhihu.com/p/405242579 import numpy as np import torch import torch.nn as nn x = torch.tensor([[[[-1, -1, -1, -1, -1, -1, -1, -1, -1], [-1, 1, -1, -1, -1, -1, -1, 1, -1], [-1, -1, 1, -1, -1, -1, 1, -1, -1], [-1, -1, -1, 1, -1, 1, -1, -1, -1], [-1, -1, -1, -1, 1, -1, -1, -1, -1], [-1, -1, -1, 1, -1, 1, -1, -1, -1], [-1, -1, 1, -1, -1, -1, 1, -1, -1], [-1, 1, -1, -1, -1, -1, -1, 1, -1], [-1, -1, -1, -1, -1, -1, -1, -1, -1]]]], dtype=torch.float) print(x.shape) print(x) print("--------------- 卷积 ---------------") conv1 = nn.Conv2d(1, 1, (3, 3), 1) # in_channel , out_channel , kennel_size , stride conv1.weight.data = torch.Tensor([[[[1, -1, -1], [-1, 1, -1], [-1, -1, 1]] ]]) conv2 = nn.Conv2d(1, 1, (3, 3), 1) # in_channel , out_channel , kennel_size , stride conv2.weight.data = torch.Tensor([[[[1, -1, 1], [-1, 1, -1], [1, -1, 1]] ]]) conv3 = nn.Conv2d(1, 1, (3, 3), 1) # in_channel , out_channel , kennel_size , stride conv3.weight.data = torch.Tensor([[[[-1, -1, 1], [-1, 1, -1], [1, -1, -1]] ]]) feature_map1 = conv1(x) feature_map2 = conv2(x) feature_map3 = conv3(x) print(feature_map1 / 9) print(feature_map2 / 9) print(feature_map3 / 9) print("--------------- 池化 ---------------") max_pool = nn.MaxPool2d(2, padding=0, stride=2) # Pooling zeroPad = nn.ZeroPad2d(padding=(0, 1, 0, 1)) # pad 0 , Left Right Up Down feature_map_pad_0_1 = zeroPad(feature_map1) feature_pool_1 = max_pool(feature_map_pad_0_1) feature_map_pad_0_2 = zeroPad(feature_map2) feature_pool_2 = max_pool(feature_map_pad_0_2) feature_map_pad_0_3 = zeroPad(feature_map3) feature_pool_3 = max_pool(feature_map_pad_0_3) print(feature_pool_1.size()) print(feature_pool_1 / 9) print(feature_pool_2 / 9) print(feature_pool_3 / 9) print("--------------- 激活 ---------------") activation_function = nn.ReLU() feature_relu1 = activation_function(feature_map1) feature_relu2 = activation_function(feature_map2) feature_relu3 = activation_function(feature_map3) print(feature_relu1 / 9) print(feature_relu2 / 9) print(feature_relu3 / 9)
这篇关于【卷积神经网络】例题XO - Python实现 - PyTorch版本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门