提交 0cf4fb23 编写于 作者: RunAtWorld's avatar RunAtWorld

使用初始化权重

上级 7b68d820
import numpy as np
from numpy import ndarray
from mydl.ch3.operation import Operation
class Dropout(Operation):
def __init__(self,
keep_prob: float = 0.8):
super().__init__()
self.keep_prob = keep_prob
def _output(self, inference: bool) -> ndarray:
if inference:
return self.input_ * self.keep_prob
else:
self.mask = np.random.binomial(1, self.keep_prob,
size=self.input_.shape)
return self.input_ * self.mask
def _input_grad(self, output_grad: ndarray) -> ndarray:
return output_grad * self.mask
import matplotlib.pyplot as plt
import numpy as np
mu, sigma = 0, 0.1 # 均值和标准差
s = np.random.normal(mu, sigma, 1000)
print(s)
print(abs(mu - np.mean(s)) < 0.01)
print(abs(sigma - np.std(s, ddof=1)) < 0.01)
count, bins, ignored = plt.hist(s, 40, color='b')
print(count, bins, ignored)
plt.plot(bins,
1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(- (bins - mu) ** 2 / (2 * sigma ** 2)),
linewidth=2,
color='r')
plt.show()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册