▲ Yann LeCun的Twitter
▲ Sebastian Raschka的Twitter:
作者:Sebastian Raschka
项目:Deep Learning Models
Star:9500+⭐
项目链接(点击阅读原文即可访问):
https://github.com/rasbt/deeplearning-models
内容:包含了由TensorFlow/PyTorch实现的80个模型,覆盖了从传统机器学习(逻辑回归、感知器等)到高阶深度网络应用(对抗生成网络等)的内容。具体如下(每个目录下有多个case):
  • 传统机器学习
  • 多层感知器
  • 卷积神经网络
  • 度量学习
  • 自编码器
  • 生成式对抗网络
  • 循环神经网络
  • 有序回归
  • 建议和技巧
  • PyTorch工作流和机制
  • TensorFlow工作流和机制
示例代码(定义感知机)
1
device = torch.device(
"cuda:0"if
 torch.cuda.is_available() 
else"cpu"
)

2
3
4defcustom_where(cond, x_1, x_2):
5return
 (cond * x_1) + ((
1
-cond) * x_2)

6
7
8classPerceptron():
9def__init__(self, num_features):
10
        self.num_features = num_features

11
        self.weights = torch.zeros(num_features, 
1

12
                                   dtype=torch.float32, device=device)

13
        self.bias = torch.zeros(
1
, dtype=torch.float32, device=device)

14
15defforward(self, x):
16
        linear = torch.add(torch.mm(x, self.weights), self.bias)

17
        predictions = custom_where(linear > 
0.
1
0
).float()

18return
 predictions

19
20defbackward(self, x, y):
21
        predictions = self.forward(x)

22
        errors = y - predictions

23return
 errors

24
25deftrain(self, x, y, epochs):
26for
 e 
in
 range(epochs):

27
28for
 i 
in
 range(y.size()[
0
]):

29# use view because backward expects a matrix (i.e., 2D tensor)
30
                errors = self.backward(x[i].view(
1
, self.num_features), y[i]).view(
-1
)

31
                self.weights += (errors * x[i]).view(self.num_features, 
1
)

32
                self.bias += errors

33
34defevaluate(self, x, y):
35
        predictions = self.forward(x).view(
-1
)

36
        accuracy = torch.sum(predictions == y).float() / y.size()[
0
]

37return
 accuracy

推荐阅读
继续阅读
阅读原文