You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MLP.py 1.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import torch.nn as nn
  2. from network_module.activation import jdlu, JDLU
  3. class MLPLayer(nn.Module):
  4. def __init__(self, dim_in, dim_out, res_coef=0.0, dropout_p=0.1):
  5. super().__init__()
  6. self.linear = nn.Linear(dim_in, dim_out)
  7. self.res_coef = res_coef
  8. self.activation = nn.ReLU()
  9. self.activation1 = JDLU(dim_out)
  10. self.dropout = nn.Dropout(dropout_p)
  11. self.ln = nn.LayerNorm(dim_out)
  12. def forward(self, x):
  13. y = self.linear(x)
  14. y = self.activation1(y)
  15. # y = jdlu(y)
  16. y = self.dropout(y)
  17. if self.res_coef == 0:
  18. return self.ln(y)
  19. else:
  20. return self.ln(self.res_coef * x + y)
  21. class MLP(nn.Module):
  22. def __init__(self, dim_in, dim, res_coef=0.5, dropout_p=0.1, n_layers=10):
  23. super().__init__()
  24. self.mlp = nn.ModuleList()
  25. self.first_linear = MLPLayer(dim_in, dim)
  26. self.n_layers = n_layers
  27. for i in range(n_layers):
  28. self.mlp.append(MLPLayer(dim, dim, res_coef, dropout_p))
  29. self.final = nn.Linear(dim, 1)
  30. def forward(self, x):
  31. x = self.first_linear(x)
  32. for layer in self.mlp:
  33. x = layer(x)
  34. x = self.final(x)
  35. return x.squeeze()

基于pytorch lightning的机器学习模板, 用于对机器学习算法进行训练, 验证, 测试等, 目前实现了神经网路, 深度学习, k折交叉, 自动保存训练信息等.

Contributors (1)