天天看點

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