model.py 1.88 KB
Newer Older
tverrbjelke's avatar
tverrbjelke committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
import numpy as np
from data_gen import gen_data
class Model:
    n_input = 100 # number is
    n_output=4
    n_h1=50
    
    def __init__(self, nodes=[n_input,n_h1, n_output]):
        
        self.nodes=nodes
        self.num_layer=len(self.nodes)
        self.weights=[]
        
        for i in range(0,self.num_layer-1):
            temp_weights=np.random.random((self.nodes[i], self.nodes[i+1]))
            self.weights.append(temp_weights)
    
    def activation(self, x, derivative=False):
        '''Activation function (sigmoid by default)
        @param x: input data
        @param derivative: boolean if we need a derivative of the sigmoid'''
 
        if not derivative:
            return 1/(1+np.exp(-x))
        else: 
            return self.activation(x, False)*(1-self.activation(x, False))
    
    def forward_step(self, x):
        
        z_array=[] # dot product solution, before activation
        a_array=[] 
        a_array.append(x) # the inner states
        outputs=[]

        #Forward propagation
                
        for i in range(0, self.num_layer-1):
            z=np.dot(self.weights[i].T,a_array[i])
            z_array.append(z)
            a=self.activation(z_array[i], False)
            a_array.append(a)
            
        outputs.append(a_array[-1])
        
        return outputs
    
    def train_all(self, X, Y):
        '''
        @param x: all 10000 reshaped matrices
        @param y: all 10000 labels
        
        
        '''
        
        for i in np.arange(0,len(X)):
            
            x=np.reshape(X[i], [100,1])
            y=Y[i]
            self.train_one(x)
                    
    def train_one(self, x):
        
        out=self.forward_step(x)
        print(out)
        sd
                
                
X, Y=gen_data()

model=Model()
model.train_all(X,Y)