▲ 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工作流和机制
示例代码(定义感知机)
1device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
2
3
4def custom_where(cond, x_1, x_2):
5 return (cond * x_1) + ((1-cond) * x_2)
6
7
8class Perceptron():
9 def __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
15 def forward(self, x):
16 linear = torch.add(torch.mm(x, self.weights), self.bias)
17 predictions = custom_where(linear > 0., 1, 0).float()
18 return predictions
19
20 def backward(self, x, y):
21 predictions = self.forward(x)
22 errors = y - predictions
23 return errors
24
25 def train(self, x, y, epochs):
26 for e in range(epochs):
27
28 for 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
34 def evaluate(self, x, y):
35 predictions = self.forward(x).view(-1)
36 accuracy = torch.sum(predictions == y).float() / y.size()[0]
37 return accuracy
原文链接: