Penalize generation of vector outside a uniform distribution

I want to create a loss to penalize when a vector z (generated by network) is generated outside a uniform distribution [-1, 1].

I suggested the following solution:

relu = torch.nn.ReLU()
loss = torch.sum(relu(torch.abs(z)-1))

Is this a proper way? is there any built-in loss function to do this?