【深度学习】Pytorch学习笔记(四)

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中的函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值