Skip to content

Commit

Permalink
sequence instance fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivamShrirao committed Jun 14, 2020
1 parent f572920 commit 49d2f37
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 45 deletions.
28 changes: 14 additions & 14 deletions cifar.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -25,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -52,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -109,7 +109,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {
"scrolled": false
},
Expand Down Expand Up @@ -174,7 +174,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -190,7 +190,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -206,7 +206,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -215,7 +215,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -224,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -236,7 +236,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 12,
"metadata": {
"scrolled": false
},
Expand All @@ -245,7 +245,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Validation Accuracy: 0.1054 - val_loss: 5.0138 - Time: 8.601s\n"
"Validation Accuracy: 0.1041 - val_loss: 4.9765 - Time: 8.761s\n"
]
}
],
Expand All @@ -257,7 +257,7 @@
"cell_type": "code",
"execution_count": 13,
"metadata": {
"scrolled": true
"scrolled": false
},
"outputs": [
{
Expand Down
3 changes: 1 addition & 2 deletions nnet_gpu/layers/BatchNormalization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from .base_layer import *
from . import seqinst
from ..stream_handler import stream_maps


Expand All @@ -15,7 +14,7 @@ def __init__(self, momentum=0.9, epsilon=1e-10, name=None):
self.name = self.__class__.__name__
else:
self.name = name
input_shape = seqinst.seq_instance.get_inp_shape()
input_shape = self.get_inp_shape()
self.shape = (None, *input_shape)
self.batches = 1
self.inp_shape = (self.batches, *input_shape)
Expand Down
2 changes: 1 addition & 1 deletion nnet_gpu/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
from .shaping import reshape
from .upsampling import upsampling

import cupyx
import cupyx
3 changes: 1 addition & 2 deletions nnet_gpu/layers/activation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from .base_layer import *
from . import seqinst
from ..stream_handler import stream_maps


Expand All @@ -14,7 +13,7 @@ def __init__(self, activation=echo, input_shape=None, name=None):
else:
self.name = name
if input_shape is None:
input_shape = seqinst.seq_instance.get_inp_shape()
input_shape = self.get_inp_shape()
self.activation = activation
self.shape = (None, *input_shape)
self.param = 0
Expand Down
4 changes: 3 additions & 1 deletion nnet_gpu/layers/base_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from ..functions import *
import numpy as np
import cupy as cp
from . import seqinst


class Layer:
Expand All @@ -24,6 +23,9 @@ def __call__(self, lyr):
lyr.output_layers.append(self)
return self

def get_inp_shape(self):
return self.input_layer.shape[1:]


class InputLayer(Layer): # just placeholder
def __init__(self, shape=None):
Expand Down
5 changes: 2 additions & 3 deletions nnet_gpu/layers/convolution/conv2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from ..base_layer import *
from .. import seqinst
from ...stream_handler import stream_maps
from .conv_utils import *

Expand All @@ -11,7 +10,7 @@ def __init__(self, num_kernels=0, input_shape=None, kernel_size=0, kernels=None,
# input_shape[row,col,channels], kernels(channels,ksz[0],ksz[1],num_kernels), biases[1,num_ker], stride[row,col]
super().__init__()
if input_shape is None:
input_shape = seqinst.seq_instance.get_inp_shape()
input_shape = self.get_inp_shape()
if name is None:
self.name = self.__class__.__name__
else:
Expand All @@ -35,7 +34,7 @@ def __init__(self, num_kernels=0, input_shape=None, kernel_size=0, kernels=None,
self.kernel_size = kernels.shape[1:3]
self.weights = self.kernels
self.bias_is_not_0 = True
if cp.isscalar(self.biases): # DO BETTER FIX
if cp.isscalar(self.biases): # TODO: DO BETTER FIX
if self.biases == 0:
self.bias_is_not_0 = False
self.dilation = dilation
Expand Down
2 changes: 1 addition & 1 deletion nnet_gpu/layers/convolution/conv2dtranspose.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def backprop(self, grads, layer=1):
with self.backp_stream:
self.backp_stream.wait_event(self.grad_event)
self.d_c_w = self.d_ker.forward(grads.transpose(3, 1, 2, 0)) # [channels,row,col,batches]
# self.d_c_w/=self.batches #take mean change over batches
# self.d_c_w/=self.batches # take mean change over batches
if layer:
d_inputs = cp.ascontiguousarray(self.d_inp.forward(grads))
# assert d_inputs.shape == (self.batches,self.row,self.col,self.channels),f"{(self.batches,self.row,self.col,self.channels)},{d_inputs.shape}"
Expand Down
3 changes: 1 addition & 2 deletions nnet_gpu/layers/dense.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from .base_layer import *
from . import seqinst
from ..stream_handler import stream_maps


Expand All @@ -15,7 +14,7 @@ def __init__(self, num_out, input_shape=None, weights=None, biases=None, activat
else:
self.name = name
if input_shape is None:
self.input_shape = seqinst.seq_instance.get_inp_shape()[0]
self.input_shape = self.get_inp_shape()[0]
else:
self.input_shape = input_shape
self.activation = activation
Expand Down
3 changes: 1 addition & 2 deletions nnet_gpu/layers/dropout.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from .base_layer import *
from . import seqinst


class dropout(Layer):
Expand All @@ -12,7 +11,7 @@ def __init__(self, rate=0.2, name=None): # rate = amount to drop
self.name = self.__class__.__name__
else:
self.name = name
input_shape = seqinst.seq_instance.get_inp_shape()
input_shape = self.get_inp_shape()
self.shape = (None, *input_shape)
self.batches = 1
self.rate = rate
Expand Down
2 changes: 1 addition & 1 deletion nnet_gpu/layers/pooling/globalAveragePool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, input_shape=None, name=None):
else:
self.name = name
if input_shape is None:
input_shape = seqinst.seq_instance.get_inp_shape()
input_shape = self.get_inp_shape()
self.param = 0
self.batches = 1
self.row, self.col, self.channels = input_shape
Expand Down
3 changes: 1 addition & 2 deletions nnet_gpu/layers/pooling/max_pool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from ..base_layer import *
from .. import seqinst
from ...stream_handler import stream_maps


Expand All @@ -19,7 +18,7 @@ def __init__(self, input_shape=None, ksize=(2, 2), stride=(2, 2), name=None):
else:
self.name = name
if input_shape is None:
input_shape = seqinst.seq_instance.get_inp_shape()
input_shape = self.get_inp_shape()
self.batches = 1
self.row, self.col, self.channels = input_shape
# self.rem_col=self.row%self.ksz
Expand Down
3 changes: 0 additions & 3 deletions nnet_gpu/layers/seqinst.py

This file was deleted.

5 changes: 2 additions & 3 deletions nnet_gpu/layers/shaping.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from .base_layer import *
from . import seqinst


class flatten(Layer):
Expand All @@ -12,7 +11,7 @@ def __init__(self, name=None):
self.name = self.__class__.__name__
else:
self.name = name
input_shape = seqinst.seq_instance.get_inp_shape()
input_shape = self.get_inp_shape()
self.r, self.c, self.channels = input_shape
self.fsz = self.r * self.c * self.channels
self.shape = (None, self.fsz)
Expand All @@ -35,7 +34,7 @@ def __init__(self, target_shape, name=None):
self.name = self.__class__.__name__
else:
self.name = name
self.input_shape = seqinst.seq_instance.get_inp_shape()
self.input_shape = self.get_inp_shape()
self.target_shape = target_shape
tt = 1
for i in self.input_shape:
Expand Down
3 changes: 1 addition & 2 deletions nnet_gpu/layers/upsampling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from .base_layer import *
from . import seqinst


class upsampling(Layer):
Expand All @@ -16,7 +15,7 @@ def __init__(self, input_shape=None, ksize=(2, 2), stride=(2, 2), name=None):
else:
self.name = name
if input_shape is None:
input_shape = seqinst.seq_instance.get_inp_shape()
input_shape = self.get_inp_shape()
self.batches = 1
self.row, self.col, self.channels = input_shape
self.out_row, self.out_col = self.row * self.ksz, self.col * self.ksz
Expand Down
10 changes: 4 additions & 6 deletions nnet_gpu/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .functions import *
from .optimizers import *
from .stream_handler import stream_maps
from .layers import Layer

import pickle
from gc import collect
Expand All @@ -11,9 +12,9 @@

# TODO- In train/fit unifunc, transpose whole data of inp at once and remove from layers.

class Sequential:
class Sequential(Layer):
def __init__(self):
layers.seqinst.seq_instance = self
super().__init__()
self.sequence = []
self.learning_rate = 0.001
self.dtype = cp.float32
Expand All @@ -23,9 +24,6 @@ def add(self, obj):
obj(self.sequence[-1])
self.sequence.append(obj)

def get_inp_shape(self):
return self.sequence[-1].shape[1:]

def forward(self, X_inp, training=True):
for obj in self.sequence:
X_inp = obj.forward(X_inp, training=training)
Expand Down Expand Up @@ -61,7 +59,7 @@ def fit(self, X_inp=None, labels=None, iterator=None, batch_size=1, epochs=1, va
sam_time = 0
for epch in range(epochs):
print("EPOCH:", epch + 1, "/", epochs)
if iterator == None:
if iterator is None:
if shuffle:
s = cp.random.permutation(lnxinp).astype(cp.int32, copy=False)
X_inp = X_inp[s]
Expand Down

0 comments on commit 49d2f37

Please sign in to comment.