造车先做三蹦子-之二:自制数据集(5x5数据集)230102

#自制数据集: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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值