天天看点

torch.nn.ReLU用法

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])