Import all the necessary modules:
In [ ]:
import torch.nn as nn
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
In [ ]:
torch.manual_seed(2)
Create a tensor ranging from -10 to 10:
In [ ]:
z=torch.arange(-10,10,0.1).view(-1, 1)
When you use sequential, you can create a sigmoid object:
In [ ]:
sig=nn.Sigmoid()
Apply the element-wise function Sigmoid with the object:
In [ ]:
yhat=sig(z)
In [ ]:
sig(torch.tensor(-1.0))
Plot the results:
In [ ]:
plt.plot(z.numpy(),yhat.numpy())
plt.xlabel('z')
plt.ylabel('yhat')
For custom modules, call the sigmoid from the torch (nn.functional for the old version), which applies the element-wise sigmoid from the function module and plots the results:
In [ ]:
yhat=torch.sigmoid(z)
plt.plot(z.numpy(),yhat.numpy())
w=torch.te
When you use sequential, you can create a tanh object:
In [ ]:
TANH=nn.Tanh()
Call the object and plot it:
In [ ]:
yhat=TANH(z)
plt.plot(z.numpy(),yhat.numpy())
For custom modules, call the Tanh object from the torch (nn.functional for the old version), which applies the element-wise sigmoid from the function module and plots the results:
In [ ]:
yhat=torch.tanh(z)
plt.plot(z.numpy(),yhat.numpy())
When you use sequential, you can create a Relu object:
In [ ]:
RELU=nn.ReLU()
yhat=RELU(z)
plt.plot(z.numpy(),yhat.numpy())
For custom modules, call the relu object from the nn.functional, which applies the element-wise sigmoid from the function module and plots the results:
In [ ]:
yhat=F.relu(z)
plt.plot(z.numpy(),yhat.numpy())
In [ ]:
x=torch.arange(-2,2,0.1).view(-1, 1)
plt.plot(x.numpy(),F.relu(x).numpy(),label='relu')
plt.plot(x.numpy(),torch.sigmoid(x).numpy(),label='sigmoid')
plt.plot(x.numpy(),torch.tanh(x).numpy(),label='tanh')
plt.legend()
Compare the activation function with a tensor in the range (-1, 1)
In [ ]:
Double-click here for the solution.
Joseph Santarcangelo has a PhD in Electrical Engineering. His research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition.
Other contributors: Michelle Carey, Mavis Zhou
Copyright © 2018 cognitiveclass.ai. This notebook and its source code are released under the terms of the MIT License.