运行环境:labview 2019 SP1,Windows 10专业版,CPU I7-11800H,内存16G
图像采用手写字符识别数据集其中两类。以三层Linear全连接网络为例
1.以pytorch框架为例
在pytorh中使用pytorch框架进行网络搭建如下所示:
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
# 定义三层全连接网络
class FCNetwork(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(FCNetwork, self).__init__()
# 第一层全连接
self.fc1 = nn.Linear(input_dim, hidden_dim)
# 第二层全连接
self.fc2 = nn.Linear(hidden_dim, hidden_dim)
# 第三层全连接
self.fc3 = nn.Linear(hidden_dim, output_dim)
# 使用PReLU激活函数
self.prelu = nn.PReLU()
def forward(self, x):
x = self.prelu(self.fc1(x)) # 第一层 + PReLU激活
x = self.prelu(self.fc2(x)) # 第二层 + PReLU激活
x = torch.sigmoid(self.fc3(x)) # 第三层 + Sigmoid激活函数