从头开始实施 dropout

Posted

技术标签:

【中文标题】从头开始实施 dropout【英文标题】:Implementing dropout from scratch 【发布时间】:2019-06-04 04:44:15 【问题描述】:

此代码尝试使用 dropout 的自定义实现:

%reset -f

import torch
import torch.nn as nn
# import torchvision
# import torchvision.transforms as transforms
import torch
import torch.nn as nn
import torch.utils.data as data_utils
import numpy as np
import matplotlib.pyplot as plt
import torch.nn.functional as F

num_epochs = 1000

number_samples = 10

from sklearn.datasets import make_moons
from matplotlib import pyplot
from pandas import DataFrame
# generate 2d classification dataset
X, y = make_moons(n_samples=number_samples, noise=0.1)
# scatter plot, dots colored by class value

x_data = [a for a in enumerate(X)]
x_data_train = x_data[:int(len(x_data) * .5)]
x_data_train = [i[1] for i in x_data_train]
x_data_train

y_data = [y[i[0]] for i in x_data]
y_data_train = y_data[:int(len(y_data) * .5)]
y_data_train

x_test = [a[1] for a in x_data[::-1][:int(len(x_data) * .5)]]
y_test = [a for a in y_data[::-1][:int(len(y_data) * .5)]]

x = torch.tensor(x_data_train).float() # <2>
print(x)

y = torch.tensor(y_data_train).long()
print(y)

x_test = torch.tensor(x_test).float()
print(x_test)

y_test = torch.tensor(y_test).long()
print(y_test)

class Dropout(nn.Module):
    def __init__(self, p=0.5, inplace=False):
#         print(p)
        super(Dropout, self).__init__()
        if p < 0 or p > 1:
            raise ValueError("dropout probability has to be between 0 and 1, "
                             "but got ".format(p))
        self.p = p
        self.inplace = inplace

    def forward(self, input):
        print(list(input.shape))
        return np.random.binomial([np.ones((len(input),np.array(list(input.shape))))],1-dropout_percent)[0] * (1.0/(1-self.p))

    def __repr__(self):
        inplace_str = ', inplace' if self.inplace else ''
        return self.__class__.__name__ + '(' \
            + 'p=' + str(self.p) \
            + inplace_str + ')'

class MyLinear(nn.Linear):
    def __init__(self, in_feats, out_feats, drop_p, bias=True):
        super(MyLinear, self).__init__(in_feats, out_feats, bias=bias)
        self.custom_dropout = Dropout(p=drop_p)

    def forward(self, input):
        dropout_value = self.custom_dropout(self.weight)
        return F.linear(input, dropout_value, self.bias)



my_train = data_utils.TensorDataset(x, y)
train_loader = data_utils.DataLoader(my_train, batch_size=2, shuffle=True)

my_test = data_utils.TensorDataset(x_test, y_test)
test_loader = data_utils.DataLoader(my_train, batch_size=2, shuffle=True)

# Device configuration
device = 'cpu'
print(device)

# Hyper-parameters 
input_size = 2
hidden_size = 100
num_classes = 2

learning_rate = 0.0001

pred = []

# Fully connected neural network with one hidden layer
class NeuralNet(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes, p):
        super(NeuralNet, self).__init__()
#         self.drop_layer = nn.Dropout(p=p)
#         self.drop_layer = MyLinear()
#         self.fc1 = MyLinear(input_size, hidden_size, p)
        self.fc1 = MyLinear(input_size, hidden_size , p) 
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size, num_classes)  

    def forward(self, x):
#         out = self.drop_layer(x)
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

model = NeuralNet(input_size, hidden_size, num_classes, p=0.9).to(device)

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)  

# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):  
        # Move tensors to the configured device
        images = images.reshape(-1, 2).to(device)
        labels = labels.to(device)

        # Forward pass
        outputs = model(images)
        loss = criterion(outputs, labels)

        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    if (epoch) % 100 == 0:
        print ('Epoch [/], Step [/], Loss: :.4f'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))

自定义 dropout 实现为:

class Dropout(nn.Module):
    def __init__(self, p=0.5, inplace=False):
#         print(p)
        super(Dropout, self).__init__()
        if p < 0 or p > 1:
            raise ValueError("dropout probability has to be between 0 and 1, "
                             "but got ".format(p))
        self.p = p
        self.inplace = inplace

    def forward(self, input):
        print(list(input.shape))
        return np.random.binomial([np.ones((len(input),np.array(list(input.shape))))],1-dropout_percent)[0] * (1.0/(1-self.p))

    def __repr__(self):
        inplace_str = ', inplace' if self.inplace else ''
        return self.__class__.__name__ + '(' \
            + 'p=' + str(self.p) \
            + inplace_str + ')'

class MyLinear(nn.Linear):
    def __init__(self, in_feats, out_feats, drop_p, bias=True):
        super(MyLinear, self).__init__(in_feats, out_feats, bias=bias)
        self.custom_dropout = Dropout(p=drop_p)

    def forward(self, input):
        dropout_value = self.custom_dropout(self.weight)
        return F.linear(input, dropout_value, self.bias)

我似乎错误地实现了 dropout 功能? :

np.random.binomial([np.ones((len(input),np.array(list(input.shape))))],1-dropout_percent)[0] * (1.0/(1-self.p))

如何修改才能正确利用dropout?

这些帖子有助于达到这一点:

Hinton 在 3 行 Python 中的 Dropout: https://iamtrask.github.io/2015/07/28/dropout/

制作自定义 Dropout 函数:https://discuss.pytorch.org/t/making-a-custom-dropout-function/14053/2

【问题讨论】:

您说您得到的印象是“似乎 [您] 错误地实现了 dropout 功能”。是什么让你这么认为? 【参考方案1】:

我的dropout功能好像实现错了?

np.random.binomial([np.ones((len(input),np.array(list(input.shape))))],1 dropout_percent)[0] * (1.0/(1-self.p))

实际上,上述实现称为Inverted Dropout。 Inverted Dropout 是在各种深度学习框架中实际实现 Dropout 的方式。

什么是倒置dropout?

在进入倒置 dropout 之前,了解 Dropout 如何作用于单个神经元会很有帮助:

由于在训练阶段,神经元以q (=1-p) 的概率保持运行,因此在测试阶段,我们必须模拟训练阶段使用的网络集合的行为。为此,作者建议在测试阶段将激活函数缩放q,以便将训练阶段产生的预期输出用作测试阶段所需的单个输出(Section 10, Multiplicative Gaussian Noise)。因此:

倒置 dropout 有点不同。这种方法包括在训练阶段对激活进行缩放,而不影响测试阶段。比例因子是保持概率1/1-p = 1/q 的倒数,因此:

反向 dropout 有助于定义模型一次,只需更改一个参数(保持/丢弃概率)即可在同一模型上运行训练和测试。相反,直接 Dropout 会强制您在测试阶段修改网络,因为如果您不乘以 q 输出,神经元将产生高于后续神经元预期值的值(因此以下神经元可以饱和或爆炸):这就是为什么 Inverted Dropout 是更常见的实现。

参考资料:

Dropout Regularization, coursera by Andrew NG

What is inverted dropout?

Dropout: scaling the activation versus inverting the dropout

Analysis of Dropout


Pytorch 如何实现倒置 dropout?

class MyDropout(nn.Module):
    def __init__(self, p: float = 0.5):
        super(MyDropout, self).__init__()
        if p < 0 or p > 1:
            raise ValueError("dropout probability has to be between 0 and 1, " "but got ".format(p))
        self.p = p

    def forward(self, X):
        if self.training:
            binomial = torch.distributions.binomial.Binomial(probs=1-self.p)
            return X * binomial.sample(X.size()) * (1.0/(1-self.p))
        return X

如何在 Numpy 中实现?

import numpy as np

pKeep = 0.8
weights = np.ones([1, 5])
binary_value = np.random.rand(weights.shape[0], weights.shape[1]) < pKeep
res = np.multiply(weights, binary_value)
res /= pKeep  # this line is called inverted dropout technique
print(res)

在 TensorFlow 中如何实现?

import tensorflow as tf
tf.enable_eager_execution()

weights = tf.ones(shape=[1, 5])
keep_prob = 0.8
random_tensor = keep_prob
random_tensor += tf.random_uniform(weights.shape)
# 0. if [keep_prob, 1.0) and 1. if [1.0, 1.0 + keep_prob)
binary_tensor = tf.floor(random_tensor)
ret = tf.div(weights, keep_prob) * binary_tensor
print(ret)

【讨论】:

在 Pytorch 实现中,为什么使用二项分布?不应该是伯努利吗? 二项式是由伯努利定义的,所以可能这就是原因。您可以在 pytorch 中使用这两个实现相同的行为。但似乎二项式可以更快。 在 PyTorch 实现中,为什么在推理过程中返回权重而不是直接返回激活 X【参考方案2】:

使用 Torch 和 bernoulli 实现..

def forward(self, x):
    output = x @ self.W.t() + self.bias
  if self.training:
    sample = torch.distributions.bernoulli.Bernoulli(self.keep_prob).sample(output.size())
    print(sample)
    return output * sample
  return output

【讨论】:

以上是关于从头开始实施 dropout的主要内容,如果未能解决你的问题,请参考以下文章

如何从头开始实施关联规则分析或市场篮子分析?

实施类似 Groupon 的系统都有哪些选择?

翻译: 3.2. 从零开始实现线性回归 深入神经网络 pytorch

简单的 vanilla RNN 没有通过梯度检查

从头开始创建 LinkedList 类

从头开始构建数据湖