【PyTorch】教程:torch.nn.PReLU

PReLU是PyTorch中的一个激活层,全称为预激活线性单元。它结合了线性项和ReLU的非线性项,其公式为PReLU(x)=max(0,x)+a*min(0,x)。参数a可以学习得到,初始值通常设置为0.25。在处理负值时,PReLU能提供更平滑的梯度,防止梯度消失问题。示例代码展示了如何创建并应用PReLU层。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

torch.nn.PReLU

原型

CLASS torch.nn.PReLU(num_parameters=1, init=0.25, device=None, dtype=None)

参数

  • num_parameters ([int]) – 需要学习的 a a a 的数量,尽管作为输入,只有两个值是合法的,1 或者 输入的通道数,默认为 1
  • init ([float]) – a a a 的初始值,默认为 0.25

定义

PReLU ( x ) = max ⁡ ( 0 , x ) + a ∗ min ⁡ ( 0 , x ) \text{PReLU}(x)= \max(0, x) + a * \min(0, x) PReLU(x)=max(0,x)+amin(0,x)

or

PReLU ( x ) = { x , if x ≥ 0 a x , otherwise \text{PReLU}(x) = \begin{cases} x, & \text{if} x \geq 0 \\ ax, & \text{otherwise} \end{cases} PReLU(x)={x,ax,ifx0otherwise

在这里插入图片描述

代码

import torch
import torch.nn as nn

m = nn.PReLU()
input = torch.randn(4)
output = m(input)

print("input: ", input)   # input:  tensor([ 0.1061, -2.0532,  1.4081, -0.1516])
print("output: ", output) # output:  tensor([ 0.1061, -0.5133,  1.4081, -0.0379], grad_fn=<PreluBackward>)

【参考】

PReLU — PyTorch 1.13 documentation

在 Spring Boot 中生成条形码,可以使用第三方库 Zxing。以下是生成条形码的示例代码: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency> ``` 2. 编写生成条形码的代码 ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @Controller @RequestMapping("/barcode") public class BarcodeController { @GetMapping("/generate") @ResponseBody public String generateBarcode(@RequestParam("code") String code) { try { int width = 200; int height = 50; String format = "png"; Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN, 2); BitMatrix bitMatrix = new MultiFormatWriter().encode(code, BarcodeFormat.CODE_128, width, height, hints); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } File file = new File("barcode." + format); ImageIO.write(image, format, file); return "Barcode generated successfully!"; } catch (WriterException | IOException e) { e.printStackTrace(); return "Error generating barcode!"; } } } ``` 3. 测试 启动 Spring Boot 应用,访问 http://localhost:8080/barcode/generate?code=123456,将会在项目根目录下生成名为 barcode.png 的条形码图片。 注意:本示例中生成的是 CODE_128 格式的条形码,你可以根据自己的需求选择不同的格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老周有AI~算法定制

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值