torch.nn.ReLU:调用relu函数
代码示例来自pytorch官方文档,正好复习下unsqueeze和cat函数的使用:

import torch
import torch.nn as nn
m=nn.ReLU()
input=torch.randn(2).unsqueeze(0)
print("input:",input)
print("input的shape:",input.shape)
output=torch.cat((m(input),m(-input)),0)
print("output:",output)
print("output的shape:",output.shape)

输出:

input: tensor([[-1.0105,  1.2734]])
input的shape: torch.Size([1, 2])
output: tensor([[0.0000, 1.2734],
        [1.0105, 0.0000]])
output的shape: torch.Size([2, 2])
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐