torch.nn.Sequential快速搭建神经网络
torch.nn.Sequential
实际仍然继承的是Module
类,其内部某些方法依然调用的是父类Module
的方法,只不过Sequential
的__init__()
可以接收用户堆叠的所有层结构(包括激活函数)的初始化操作,非常方便。
import torch
import warnings
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification, make_moons
warnings.filterwarnings("ignore")
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
random_state=1, n_clusters_per_class=1)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
linearly_separable = (X, y)
X, y = make_moons(noise=0.3, random_state=0)
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
x_train, x_test, y_train, y_test = torch.from_numpy(x_train).type(torch.FloatTensor), torch.from_numpy(x_test).type(torch.FloatTensor), torch.from_numpy(y_train).type(torch.LongTensor), torch.from_numpy(y_test).type(torch.LongTensor)
plt.scatter(x_train[:, 0], x_train[:, 1], c=y_train, cmap=cm_bright,
edgecolors='k', s=80)
plt.scatter(x_test[:, 0], x_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.5,
edgecolors='k', s=80)
plt.show()
model = torch.nn.Sequential(
torch.nn.Linear(2, 50),
torch.nn.ReLU(),
torch.nn.Linear(50, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 2),
torch.nn.Sigmoid()
)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-2) # 定义优化器
loss_func = torch.nn.CrossEntropyLoss() # 定义损失函数为交叉熵
print("Training-------------------")
for t in range(1000):
classification = model(x_train) # 实例化模型对象
loss = loss_func(classification, y_train) # 计算交叉熵
optimizer.zero_grad() # 梯度清零
loss.backward() # 实现后向传播,计算各参数梯度
optimizer.step() # 更新各参数的值
if t % 5 == 0:
print("CrossEntropy: ", loss.data.numpy())
print("Testing--------------------")
classification_pre = model(x_test)
y_test_pre = classification_pre.argmax(dim=1).numpy() # 寻找概率最大的列索引
print("Testing Accuracy: ", accuracy_score(y_test_pre, y_test.numpy())) # 计算正确率
这里的torch.nn.Relu()
以及torch.nn.Sigmoid()
是类类型,其内部依然调用的是torch.nn.functional
中的函数。