手写relu激活函数(numpy、torch)

Numpy:

import numpy as np
x = np.random.randn(100)
x_relu = np.maximum(x,0)

Pytorch:

import torch
x = torch.randn(100)
x_relu = torch.clamp(x,min = 0)

关于torch.clamp:
在这里插入图片描述
examples:

a = torch.randn(4)
a
>>>tensor([-1.7120,  0.1734, -0.0478, -0.0922])
torch.clamp(a, min=-0.5, max=0.5)
>>>tensor([-0.5000,  0.1734, -0.0478, -0.0922])

把一个tensor"夹"进规定的范围内。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是基于PyTorch构建手写数字识别模型的代码。首先需要导入相关的包和库。我们需要使用torchtorchvision来构建模型和数据集,使用numpy来进行数据处理。 ```python import torch import torchvision import torchvision.transforms as transforms import numpy as np ``` 接下来,我们需要定义一些超参数,例如学习率、批次大小、训练轮数等等。 ```python learning_rate = 0.001 batch_size = 100 num_epochs = 10 ``` 然后,我们需要加载手写数字数据集MNIST,这个数据集已经被预处理成一个28x28的图像和一个标签。图像是灰度图像,所以只有一个通道。 ```python # 加载训练集和测试集 train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True) test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor(), download=True) # 加载数据集 train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False) ``` 接下来,我们需要定义卷积神经网络的结构。这里我们使用两个卷积层和两个池化层,然后是一个全连接层和一个输出层。在每个卷积层和全连接层之后都使用ReLU激活函数。 ```python class ConvNet(torch.nn.Module): def __init__(self): super(ConvNet, self).__init__() self.layer1 = torch.nn.Sequential( torch.nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=2), torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2)) self.layer2 = torch.nn.Sequential( torch.nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2), torch.nn.ReLU(), torch.nn.MaxPool2d(kernel_size=2, stride=2)) self.fc1 = torch.nn.Linear(7*7*64, 1000) self.fc2 = torch.nn.Linear(1000, 10) def forward(self, x): out = self.layer1(x) out = self.layer2(out) out = out.reshape(out.size(0), -1) out = self.fc1(out) out = self.fc2(out) return out model = ConvNet() ``` 接下来,我们需要定义损失函数和优化器。这里我们使用交叉熵损失函数和Adam优化器。 ```python criterion = torch.nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) ``` 然后,我们可以开始训练模型。每个批次数据会被送入网络中进行前向传播、计算损失、反向传播和更新参数。训练过程中,我们会输出每个批次数据的损失和准确率。 ```python # 训练模型 total_step = len(train_loader) for epoch in range(num_epochs): for i, (images, labels) in enumerate(train_loader): # 前向传播 outputs = model(images) loss = criterion(outputs, labels) # 反向传播和优化 optimizer.zero_grad() loss.backward() optimizer.step() # 每100个批次数据输出一次状态 if (i+1) % 100 == 0: print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item())) # 测试模型 model.eval() with torch.no_grad(): correct = 0 total = 0 for images, labels in test_loader: outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total)) ``` 最后,我们可以使用精确率、召回率和F1值来评估模型的性能。 ```python # 计算精确率、召回率和F1值 model.eval() with torch.no_grad(): correct = 0 total = 0 true_positives = 0 false_positives = 0 false_negatives = 0 for images, labels in test_loader: outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() true_positives += ((predicted == labels) & (predicted == 1)).sum().item() false_positives += ((predicted != labels) & (predicted == 1)).sum().item() false_negatives += ((predicted != labels) & (predicted == 0)).sum().item() precision = true_positives / (true_positives + false_positives) recall = true_positives / (true_positives + false_negatives) f1_score = 2 * (precision * recall) / (precision + recall) print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total)) print('Precision of the model on the test images: {:.4f}'.format(precision)) print('Recall of the model on the test images: {:.4f}'.format(recall)) print('F1 Score of the model on the test images: {:.4f}'.format(f1_score)) ``` 这就是使用PyTorch构建基于卷积神经网络的手写数字识别模型的完整代码,希望对你有帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值