示例#1
0
    def __init__(self, X_train, Y_train, Net='LeNet5', opti='SGDMomentum'):
        # Prepare Data: Load, Shuffle, Normalization, Batching, Preprocessing
        self.X_train = X_train
        self.Y_train = Y_train

        self.batch_size = 64
        # D_in: input depth of network, 784, 28*28 input grayscale image
        self.D_in = 784
        # D_out: output depth of network = 10, the 10 digits
        self.D_out = 10

        print('  Net: ' + str(Net))
        print('  batch_size: ' + str(self.batch_size))
        print('  D_in: ' + str(self.D_in))
        print('  D_out: ' + str(self.D_out))
        print('  Optimizer: ' + opti)

        # =======================
        if Net == 'TwoLayerNet':
            # H is the size of the one hidden layer.
            H = 400
            self.model = ANN.TwoLayerNet(self.D_in, H, self.D_out)
        elif Net == 'ThreeLayerNet':
            #######################################
            ############  TODO   ##################
            #######################################
            # H1, H2 are the size of the two hidden layers.
            #self.model = ANN.ThreeLayerNet (self.D_in, H1, H2, self.D_out)
            print('Not Implemented.')
            exit(0)
        elif Net == 'LeNet5':
            self.model = CNN.LeNet5()

        # store training loss over iterations, for later visualization
        self.losses = []

        if opti == 'SGD':
            self.opti = optimizer.SGD(self.model.get_params(),
                                      lr=0.0001,
                                      reg=0)
        else:
            self.opti = optimizer.SGDMomentum(self.model.get_params(),
                                              lr=0.0001,
                                              momentum=0.80,
                                              reg=0.00003)

        self.criterion = loss.CrossEntropyLoss()
示例#2
0
DimOutputData = 10  #the dimension of OutputData

print("The batch size of train: " + str(BatchSize) +
      ", the dimension of InputData: " + str(DimInputData) +
      ", the dimension of OutputData: " + str(DimOutputData))

### Normal Three Layer Neural Network Test ###
#construct the model of nn
FirstLayerNum = 500  #the first layer of nn has 500 neuron
SecondLayerNum = 200  #the second layer of nn has 200 neuron
NetWorkModel = network.ThreeLayerNet(BatchSize, DimInputData, FirstLayerNum,
                                     SecondLayerNum, DimOutputData)
#set the training part of nn
Losses = []
SGDOptimizer = optimizer.SGDMomentum(NetWorkModel.get_params(),
                                     lr=0.0001,
                                     momentum=0.80,
                                     reg=0.00003)
LossFunction = lossfunction.CrossEntropyLoss()

#the train process of nn
IterationNum = 25000
for i in range(IterationNum + 1):
    TrainBatch, LabelBatch = plugin.GetBatch(TrainData, TrainLabel, BatchSize)
    LabelBatch = plugin.MakeLabel(LabelBatch, DimOutputData)

    LabelPred = NetWorkModel.forward(TrainBatch)
    Loss, Dout = LossFunction.get(LabelPred, LabelBatch)
    NetWorkModel.backward(Dout)
    SGDOptimizer.step()

    if i % 100 == 0:
示例#3
0
D_out = 10

print("batch_size: " + str(batch_size) + ", D_in: " + str(D_in) + ", D_out: " +
      str(D_out))

### TWO LAYER NET FORWARD TEST ###
#H=400
#model = nn.TwoLayerNet(batch_size, D_in, H, D_out)
H1 = 300
H2 = 100
model = nn.ThreeLayerNet(batch_size, D_in, H1, H2, D_out)

losses = []
#optim = optimizer.SGD(model.get_params(), lr=0.0001, reg=0)
optim = optimizer.SGDMomentum(model.get_params(),
                              lr=0.0001,
                              momentum=0.80,
                              reg=0.00003)
criterion = loss.CrossEntropyLoss()

# TRAIN
ITER = 25000
for i in range(ITER):
    # get batch, make onehot
    X_batch, Y_batch = util.get_batch(X_train, Y_train, batch_size)
    Y_batch = util.MakeOneHot(Y_batch, D_out)

    # forward, loss, backward, step
    Y_pred = model.forward(X_batch)
    loss, dout = criterion.get(Y_pred, Y_batch)
    model.backward(dout)
    optim.step()