#自制数据集:5*5的点阵输入,数字的输出9个OneHot输出……Jupyter Notebook231001
import torch #pytorch
import torch.nn as nn
import torch.optim as optim #优化器optimizer
# 定义模型
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(25, 50)
self.fc2 = nn.Linear(50, 9)
def forward(self, x):
x = x.view(-1, 25)
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
# 训练数据
i = [
#0
torch.tensor([[1,1,1,1,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,1,1,1,1]], dtype=torch.float32),
#1
torch.tensor([[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0]], dtype=torch.float32),
#2
torch.tensor([[0,0,0,0,0],
[0,0,0,0,0],
[1,1,1,1,1],
[0,0,0,0,0],
[0,0,0,0,0]], dtype=torch.float32),
torch.tensor([[0,0,1,0,0],
[0,0,1,0,0],
[1,1,1,1,1],
[0,0,1,0,0],
[0,0,1,0,0]], dtype=torch.float32),
torch.tensor([[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1]], dtype=torch.float32),
torch.tensor([[1,1,1,1,1],
[1,0,0,0,0],
[1,1,1,1,1],
[0,0,0,0,1],
[1,1,1,1,1]], dtype=torch.float32),
torch.tensor([[1,1,0,0,0],
[1,0,0,0,0],
[1,1,1,1,0],
[1,0,0,0,1],
[1,1,1,1,1]], dtype=torch.float32),
#7
torch.tensor([[0,1,1,1,1],
[0,0,0,1,1],
[0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,1,1]], dtype=torch.float32),
#数字8
torch.tensor([[1,1,1,1,1],
[1,0,0,0,1],
[1,1,1,1,1],
[1,0,0,0,1],
[1,1,1,1,1]], dtype=torch.float32),
# ...其他数据
]
target = [torch.tensor([1,0,0,0,0,0,0,0,0]), #0
torch.tensor([0,1,0,0,0,0,0,0,0]), #1
torch.tensor([0,0,1,0,0,0,0,0,0]), #2
torch.tensor([0,0,0,1,0,0,0,0,0]), #3
torch.tensor([0,0,0,0,1,0,0,0,0]), #4
torch.tensor([0,0,0,0,0,1,0,0,0]),
torch.tensor([0,0,0,0,0,0,1,0,0]), #6
torch.tensor([0,0,0,0,0,0,0,1,0]), #7
torch.tensor([0,0,0,0,0,0,0,0,1]), #8
# ...其他目标
]
# 测试数据
ib = [
#预计数字:"0"
torch.tensor([[1,1,1,1,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,1,1],
[1,1,1,1,2]], dtype=torch.float32),
#预计6
torch.tensor([[6,1,0,0,0],
[6,0,0,0,0],
[6,1,1,0,0],
[6,0,0,1,0],
[6,6,6,1,0]], dtype=torch.float32),
torch.tensor([[0,0,0,0,0],
[0,1,1,1,1],
[1,1,1,1,1],
[0,0,0,0,0],
[0,0,0,0,0]], dtype=torch.float32),
torch.tensor([[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,1,0],
[0,0,1,0,0]], dtype=torch.float32),
torch.tensor([[0,0,1,0,0],
[0,0,1,0,0],
[1,1,1,1,1],
[0,0,1,0,0],
[0,0,1,0,0]], dtype=torch.float32),
torch.tensor([[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1]], dtype=torch.float32),
torch.tensor([[1,1,1,1,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0.5,1],
[1,1,1,1,2]], dtype=torch.float32),
#预计"7"
torch.tensor([[0,1,1,1,0],
[0,0,0,1,0],
[0,0,0.05,0,0],
[0,0,0,1,0],
[0,0,1,0,0]], dtype=torch.float32),
#预计数字:"8"
torch.tensor([[1,1,1,1,1],
[1,0,0,0,1],
[1,1,1,1,1],
[1,0,0.5,0.9, 1.2],
[1,1,1,2,3]], dtype=torch.float32),
]
# 初始化网络、损失函数和优化器
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)
# 训练模型
for epoch in range(100):
for inputs, labels in zip(i, target):
optimizer.zero_grad()
inputs = inputs.unsqueeze(0) # 增加批次维度
outputs = net(inputs)
loss = criterion(outputs, torch.argmax(labels.unsqueeze(0), 1))
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1}, Loss: {loss.item()}")
print("Training complete!")
# 使用测试数据进行测试
with torch.no_grad():
for test_input in ib:
test_input = test_input.unsqueeze(0) # 增加批次维度
outputs = net(test_input)
prediction = torch.argmax(outputs, 1)
print(f"Test input:\n{test_input.squeeze(0)}\nPredicted class: {prediction.item()}\n")
#Jupyter Notebook231001
import torch
import torch.nn as nn
import torch.optim as optim
# 定义模型
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(25, 50)
self.fc2 = nn.Linear(50, 6)
def forward(self, x):
x = x.view(-1, 25)
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
# 训练数据
i = [torch.tensor([[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0]], dtype=torch.float32),
torch.tensor([[0,0,0,0,0],
[0,0,0,0,0],
[1,1,1,1,1],
[0,0,0,0,0],
[0,0,0,0,0]], dtype=torch.float32),
torch.tensor([[0,0,1,0,0],
[0,0,1,0,0],
[1,1,1,1,1],
[0,0,1,0,0],
[0,0,1,0,0]], dtype=torch.float32),
torch.tensor([[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1]], dtype=torch.float32),
torch.tensor([[1,1,1,1,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,1,1,1,1]], dtype=torch.float32),
torch.tensor([[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]], dtype=torch.float32),
# ...其他数据
]
target = [torch.tensor([1,0,0,0,0,0]),
torch.tensor([0,1,0,0,0,0]),
torch.tensor([0,0,1,0,0,0]),
torch.tensor([0,0,0,1,0,0]),
torch.tensor([0,0,0,0,1,0]),
torch.tensor([0,0,0,0,0,1]),
# ...其他目标
]
# 测试数据
ib = [torch.tensor([[0,0,0,0,0],
[0,1,1,1,1],
[1,1,1,1,1],
[0,0,0,0,0],
[0,0,0,0,0]], dtype=torch.float32),
torch.tensor([[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,0,0],
[0,0,1,1,0],
[0,0,1,0,0]], dtype=torch.float32),
torch.tensor([[0,0,1,0,0],
[0,0,1,0,0],
[1,1,1,1,1],
[0,0,1,0,0],
[0,0,1,0,0]], dtype=torch.float32),
torch.tensor([[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1]], dtype=torch.float32),
torch.tensor([[1,1,1,1,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0.5,1],
[1,1,1,1,2]], dtype=torch.float32),
torch.tensor([[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0.05,0,0],
[0,0,0,0,0],
[0,0,0,0,0]], dtype=torch.float32),
]
# 初始化网络、损失函数和优化器
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)
# 训练模型
for epoch in range(100):
for inputs, labels in zip(i, target):
optimizer.zero_grad()
inputs = inputs.unsqueeze(0) # 增加批次维度
outputs = net(inputs)
loss = criterion(outputs, torch.argmax(labels.unsqueeze(0), 1))
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1}, Loss: {loss.item()}")
print("Training complete!")
# 使用测试数据进行测试
with torch.no_grad():
for test_input in ib:
test_input = test_input.unsqueeze(0) # 增加批次维度
outputs = net(test_input)
prediction = torch.argmax(outputs, 1)
print(f"Test input:\n{test_input.squeeze(0)}\nPredicted class: {prediction.item()}\n")