def __init__(self, tau, T, v_threshold=1.0, v_reset=0.0): super().__init__() self.T = T self.static_conv = nn.Sequential( nn.Conv2d(1, 128, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(128), ) self.conv = nn.Sequential( neuron.IFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan()), nn.MaxPool2d(2, 2), # 14 * 14 nn.Conv2d(128, 128, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(128), neuron.IFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan()), nn.MaxPool2d(2, 2) # 7 * 7 ) self.fc = nn.Sequential( nn.Flatten(), layer.Dropout(0.7), nn.Linear(128 * 7 * 7, 128 * 3 * 3, bias=False), neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan()), layer.Dropout(0.7), nn.Linear(128 * 3 * 3, 128, bias=False), neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan()), nn.Linear(128, 10, bias=False), neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan()), )
def __init__(self, channels: int = 128): super().__init__() conv = [] conv.extend(SJSNN.conv3x3(2, channels)) conv.append(nn.MaxPool2d(2, 2)) for i in range(4): conv.extend(SJSNN.conv3x3(channels, channels)) conv.append(nn.MaxPool2d(2, 2)) self.conv = nn.Sequential(*conv) self.fc = nn.Sequential( nn.Flatten(), layer.Dropout(0.5), nn.Linear(channels * 4 * 4, channels * 2 * 2, bias=False), neuron.LIFNode(tau=2.0, surrogate_function=surrogate.ATan(), detach_reset=True), layer.Dropout(0.5), nn.Linear(channels * 2 * 2, 110, bias=False), neuron.LIFNode(tau=2.0, surrogate_function=surrogate.ATan(), detach_reset=True)) self.vote = VotingLayer(10)
def __init__(self, in_c = 1,nf = [16,32], ks=[5,5], T = 10, v_threshold = 1.0, v_reset = 0.0, dropout=0.5, use_softmax = False, tau=2.0, lif=False): super().__init__() self.T = T self.use_softmax = use_softmax self.static_conv = nn.Sequential( nn.Conv2d(in_c, nf[0], kernel_size=ks[0], bias=False), nn.BatchNorm2d(nf[0]) ) self.dim1 = (28 - ks[0] + 1) // 2 self.conv = nn.Sequential( neuron.IFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan(), detach_reset=True), nn.MaxPool2d(2, 2), nn.Conv2d(nf[0], nf[1], kernel_size=ks[1], bias=False), nn.BatchNorm2d(nf[1]), neuron.IFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan(), detach_reset=True), nn.MaxPool2d(2, 2) ) if lif: self.conv[0] = neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan()) self.conv[4] = neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan()) self.dim2 = (self.dim1 - ks[1] + 1) // 2 self.fc = nn.Sequential( nn.Flatten(), layer.Dropout(dropout), nn.Linear(nf[1]*self.dim2**2, 10, bias=False), neuron.IFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan(), detach_reset=True) ) if lif: self.fc[-1] = neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan()) if self.use_softmax: self.softmax = nn.Softmax(dim=1)
def __init__(self, T=8, v_threshold=1.0, v_reset=0.0, tau=2.0, surrogate_function=surrogate.ATan()): super().__init__() self.train_times = 0 self.epochs = 0 self.max_test_acccuracy = 0 self.T = T self.static_conv = nn.Sequential( nn.Conv2d(3, 256, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(256), ) self.conv = nn.Sequential( neuron.LIFNode(v_threshold=v_threshold, v_reset=v_reset, tau=tau, surrogate_function=surrogate_function, detach_reset=True), nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(256), neuron.LIFNode(v_threshold=v_threshold, v_reset=v_reset, tau=tau, surrogate_function=surrogate_function, detach_reset=True), nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(256), neuron.LIFNode(v_threshold=v_threshold, v_reset=v_reset, tau=tau, surrogate_function=surrogate_function, detach_reset=True), nn.MaxPool2d(2, 2), # 16 * 16 nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(256), neuron.LIFNode(v_threshold=v_threshold, v_reset=v_reset, tau=tau, surrogate_function=surrogate_function, detach_reset=True), nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(256), neuron.LIFNode(v_threshold=v_threshold, v_reset=v_reset, tau=tau, surrogate_function=surrogate_function, detach_reset=True), nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(256), neuron.LIFNode(v_threshold=v_threshold, v_reset=v_reset, tau=tau, surrogate_function=surrogate_function, detach_reset=True), nn.MaxPool2d(2, 2) # 8 * 8 ) self.fc = nn.Sequential( nn.Flatten(), layer.Dropout(0.5), nn.Linear(256 * 8 * 8, 128 * 4 * 4, bias=False), neuron.LIFNode(v_threshold=v_threshold, v_reset=v_reset, tau=tau, surrogate_function=surrogate_function, detach_reset=True), nn.Linear(128 * 4 * 4, 100, bias=False), neuron.LIFNode(v_threshold=v_threshold, v_reset=v_reset, tau=tau, surrogate_function=surrogate_function, detach_reset=True)) self.boost = nn.AvgPool1d(10, 10)
def __init__(self): super().__init__() self.train_epoch = 0 self.cnn11 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False) self.lif11 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.avgpool1 = nn.AvgPool2d(kernel_size=2) self.if1 = IFNode() self.cnn21 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1, bias=False) self.lif21 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.cnn22 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1, bias=False) self.shortcut1 = nn.Sequential(nn.Conv2d(64, 128, kernel_size=1, stride=1, bias=False),) self.lif2 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.cnn31 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1, bias=False) self.lif31 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.cnn32 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=2, padding=1, bias=False) self.shortcut2 = nn.Sequential(nn.Conv2d(128, 256, kernel_size=1, stride=2, bias=False),) self.lif3 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.cnn41 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1, padding=1, bias=False) self.lif41 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.cnn42 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1, bias=False) self.shortcut3 = nn.Sequential(nn.Conv2d(256, 512, kernel_size=1, stride=1, bias=False),) self.lif4 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.cnn51 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1, bias=False) self.lif51 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.cnn52 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=2, padding=1, bias=False) self.shortcut4 = nn.Sequential(nn.AvgPool2d(kernel_size=(1, 1), stride=(2, 2), padding=(0, 0))) self.lif5 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.fc0 = nn.Linear(512 * 4 * 4, 1024, bias=False) self.lif6 = nn.Sequential( LIFNode(), layer.Dropout(0.25) ) self.fc1 = nn.Linear(1024, 10, bias=False) self.lif_out = LIFNode(fire=False) for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels variance1 = math.sqrt(1.0 / n) m.weight.data.normal_(0, variance1) elif isinstance(m, nn.Linear): size = m.weight.size() fan_in = size[1] variance2 = math.sqrt(1.0 / fan_in) m.weight.data.normal_(0.0, variance2)