Exemple #1
0
 def __init__(self, nnModel=None, nnTestData=None):
     """
     Define the model structure
     """
     self.nnModel = nnModel
     if (nnModel is None):
         l1 = NNLayer(3,
                      activation=NNLayer.A_Linear,
                      layerType=NNLayer.T_Input)
         l2 = NNLayer(2,
                      activation=NNLayer.A_Linear,
                      layerType=NNLayer.T_Hidden)
         l3 = NNLayer(1,
                      activation=NNLayer.A_Linear,
                      layerType=NNLayer.T_Output)
         c1 = NNConnection(l1, l2)
         c2 = NNConnection(l2, l3)
         #c1 = NNConnection(l1,l3)
         self.nnModel = NNModel()
         self.nnModel.addConnection(c1)
         self.nnModel.addConnection(c2)
         """
         Create the test data using the number of nodes in the InputLayer for the col
         dimension of the X input
         """
         # test data
         self.m = 100
         self.n = nnModel.getInputLayer().getNodeCnt()
         self.x = rng.rand(self.m, self.n)
         # the larger
         self.w = np.linspace(0, 3, self.n) + 1.0
         self.b = 3.5
         #err = rng.randn(m)
         z = np.dot(self.x, np.transpose(self.w))
         self.y = np.asmatrix(z + self.b).reshape(self.m, 1)
     else:
         if (nnTestData is None):
             raise ValueError(
                 "TestData must be supplied when not using a default nnModel value"
             )
         # transfer test data to gradient tester
         self.m = nnTestData.m
         self.n = nnTestData.n
         self.x = nnTestData.x
         self.w = nnTestData.w
         self.b = nnTestData.b
         self.y = nnTestData.y
Exemple #2
0
X = iris.data
Y = iris.target
Yd = pd.get_dummies(Y)
Y = Yd.as_matrix()

# Test data
m = X.shape[0]
n = X.shape[1]
numClasses = Y.shape[1]

# Always make sure the input and output layers and their connections
# are the first and last in the list
lIn = NNLayer(n,activation=NNLayer.A_Tanh, layerType=NNLayer.T_Input)
lH1 = NNLayer(20,activation=NNLayer.A_Tanh, layerType=NNLayer.T_Hidden)
lOut = NNLayer(numClasses,activation=NNLayer.A_Softmax, layerType=NNLayer.T_Output)
c1 = NNConnection(lIn,lH1)
c2 = NNConnection(lH1,lOut)

#c1 = NNConnection(l1,l3)
nnModel = NNModel()
nnModel.addConnection(c1)
nnModel.addConnection(c2)

# Build the controller and train the model
nnController = NNController(nnModel)

#yh = nnModel.forwardPropigation(X, Y)
#cost = nnModel.getCost(Y)


# Train the model
sys.path.append('C:/Users/tschlosser/Google Drive/Python/NNModel')

import numpy as np
import matplotlib.pyplot as plt
rng = np.random
from NNLayer import NNLayer
from NNConnection import NNConnection
from NNModel import NNModel
from NNController import NNController
import NNFunctions as nnf

# Always make sure the input and output layers and their connections
# are the first and last in the list
lIn = NNLayer(2, layerType=NNLayer.T_Input)
lOut = NNLayer(1,activation=NNLayer.A_Sigmoid, layerType=NNLayer.T_Output)
c1 = NNConnection(lIn,lOut)

#c1 = NNConnection(l1,l3)
nnModel = NNModel()
nnModel.addConnection(c1)

# Test data
m = 200
n = lIn.getNodeCnt()
x = 10*(rng.rand(m,n) - 0.5)

# the larger
w = np.linspace(-6,6,n) + 1.0
b = -3.5
err = 0 #10*rng.randn(m)
z = np.asmatrix(np.dot(x,np.transpose(w)) + b + err).reshape(m,1)
# from reaching zero and the cost baseline tends to be related to the noise
# value
x, y, coef = datasets.make_regression(n_samples=m,
                                      n_features=n,
                                      n_informative=1,
                                      noise=10,
                                      coef=True,
                                      random_state=None)
y = np.asmatrix(y).reshape(m, 1)

# Always make sure the input and output layers and their connections
# are the first and last in the list
l1 = NNLayer(n, activation=NNLayer.A_Linear, layerType=NNLayer.T_Input)
l2 = NNLayer(3, activation=NNLayer.A_Linear, layerType=NNLayer.T_Hidden)
l3 = NNLayer(1, activation=NNLayer.A_Linear, layerType=NNLayer.T_Output)
c1 = NNConnection(l1, l2)
c2 = NNConnection(l2, l3)
#c1 = NNConnection(l1,l3)
nnModel = NNModel()
nnModel.addConnection(c1)
nnModel.addConnection(c2)

# save initial params
nnParams = nnModel.getModelParams()
nnModel.setModelParams(nnParams)
'''
Run SG
'''
nnController = NNController(nnModel)
begTime = dt.datetime.now()
results = nnController.trainModelSGD(x,