Beispiel #1
0
    def __init__(self, input_size, mid_size, out_size, sig=True):
        mag = None
        if sig:
            mag = 1
        else:
            mag = 2
        self.weights = {
            'W1':
            np.random.normal(0, mag / np.sqrt(input_size),
                             (input_size, mid_size)),
            'b1':
            np.random.normal(0, mag / np.sqrt(input_size), (mid_size, )),
            'W2':
            np.random.normal(0, mag / np.sqrt(mid_size), (mid_size, out_size)),
            'b2':
            np.random.normal(0, mag / np.sqrt(mid_size), (out_size, ))
        }

        self.layers = OrderedDict()
        self.layers['Affine1'] = layers.Affine(self.weights['W1'],
                                               self.weights['b1'])
        if sig:
            self.layers['Sig'] = layers.Sigmoid()
        else:
            self.layers['ReLU'] = layers.ReLU()
        self.layers['Dropout'] = layers.Dropout()
        self.layers['Affine2'] = layers.Affine(self.weights['W2'],
                                               self.weights['b2'])
        self.last_layer = layers.SmLo()
    def __init__(self, input_dim=(1,28,28),
                 conv_param={'filter_num':30, 'filter_size':5, 'pad':0, 'stride':1},
                 hidden_size=100, output_size=10, weight_init_std=0.01):
        filter_num = conv_param['filter_num']
        filter_size = conv_param['filter_size']
        filter_pad = conv_param['pad']
        filter_stride = conv_param['stride']

        input_size = input_dim[1]
        conv_output_size = (input_size - filter_size + 2 * filter_pad) / filter_stride + 1
        pool_output_size = int(filter_num * (conv_output_size/2) * (conv_output_size/2))

        self.params={}
        self.params['W1'] = weight_init_std * np.random.randn(filter_num, input_dim[0], filter_size, filter_size)
        self.params['b1'] = np.zeros(filter_num)
        self.params['W2'] = weight_init_std * np.random.randn(pool_output_size, hidden_size)
        self.params['b2'] = np.zeros(hidden_size)
        self.params['W3'] = weight_init_std * np.random.randn(hidden_size, output_size)
        self.params['b3'] = np.zeros(output_size)

        self.layers = OrderedDict()
        self.layers['Conv1'] = conv.Convolution(self.params['W1'], self.params['b1'], filter_stride, filter_pad)
        self.layers['ReLU1'] = Layers.ReLU()
        self.layers['Pool1'] = conv.Pool(2,2,2)
        self.layers['Affine1'] =Layers.Affine(self.params['W2'], self.params['b2'])
        self.layers['ReLU2'] = Layers.ReLU()
        self.layers['Affine2'] = Layers.Affine(self.params['W3'], self.params['b3'])
        self.last_layer = Layers.SoftmaxWithLoss()
Beispiel #3
0
    def __init__(self, input_size, hidden_size, output_size, init_weight_std=0.01):
        self.params = dict()
        self.params["W1"] = init_weight_std * np.random.rand(input_size, hidden_size)
        self.params["b1"] = np.zeros(hidden_size)
        self.params["W2"] = init_weight_std * np.random.rand(hidden_size, output_size)
        self.params["b2"] = np.zeros(output_size)

        self.layers = OrderedDict()
        self.layers["Affine1"] = L.Affine(self.params["W1"], self.params["b1"])
        self.layers["Activation1"] = L.Relu()
        self.layers["Affine2"] = L.Affine(self.params["W2"], self.params["b2"])
        self.layers["Activation2"] = L.Relu()

        self.output_layer = L.Softmax_with_loss()
Beispiel #4
0
def fc_bn_dropout(inp_layer, size, training_name):
    fc = layers.Affine(size, inp_layer)
    fc = layers.Batchnorm(size[1], training_name, fc)
    fc = layers.Relu(fc)
    fc = layers.Dropout(0.8, training_name, fc)

    return fc
Beispiel #5
0
    def __init__(self, input_size, hidden_size, output_size, weight_init_std):
        #parameter
        self.params = {}
        self.params["W1"] = weight_init_std * np.random.randn(
            input_size, hidden_size)
        self.params["b1"] = weight_init_std * np.random.randn(hidden_size)
        self.params["W2"] = weight_init_std * np.random.randn(
            hidden_size, output_size)
        self.params["b2"] = weight_init_std * np.random.randn(output_size)

        #layer
        self.layers = OrderedDict()
        self.layers["affine1"] = ly.Affine(self.params["W1"],
                                           self.params["b1"])
        self.layers["Sigmoid"] = ly.SigmoidLayer()
        self.layers["affine2"] = ly.Affine(self.params["W2"],
                                           self.params["b2"])
        self.lastlayer = ly.SoftmaxwithLoss()
Beispiel #6
0
    def __init__(self, input_size, output_size, init_weight_std=0.01, filter_num=5, filter_size=3 ,pool_size = 2):
        # input = [C, h, w]
        size = input_size[1]
        conv_out_size = (size - filter_size + 1)
        pool_out_size = int((conv_out_size/2)**2*filter_num)


        self.params = dict()
        self.params["W1"] = init_weight_std * np.random.rand(filter_num,input_size[0], filter_size, filter_size)
        self.params["b1"] = np.zeros(filter_num)
        self.params["W2"] = init_weight_std * np.random.rand(pool_out_size, output_size)
        self.params["b2"] = np.zeros(output_size)

        self.layers = OrderedDict()
        self.layers["Conv"] = L.Convolution(self.params["W1"], self.params["b1"])
        self.layers["Activation1"] = L.Relu()
        self.layers["Pooling"] = L.Pooling(pool_size,pool_size,stride=2)
        self.layers["Affine1"] = L.Affine(self.params["W2"], self.params["b2"])
        self.layers["Activation2"] = L.Relu()

        self.output_layer = L.Softmax_with_loss()
        self.y = None
Beispiel #7
0
def my_conv_net(n_classes):

    # initialization
    training_name = 'mode'
    loss_name = 'loss'
    ground_truth = 'y'

    input_layer = layers.Input()
    inp = layers.L2Norm(0.01, loss_name, input_layer)

    # Convoluton layers
    conv_count = 3
    res_shape = [(32, 32), (16, 16), (8, 8)]
    conv_filters = [(32, 32), (128, 128), (256, 512)]
    conv_shapes = [((3, 3, 3), (3, 3)), ((32, 3, 3), (3, 3)),
                   ((128, 3, 3), (3, 3))]
    for i in range(0, conv_count):
        inp = conv_bn_conv_bn_pool2x2(inp, conv_filters[i], conv_shapes[i],
                                      res_shape[i], training_name)

    flat = 4 * 4 * 512
    inp = layers.Reshape((flat, ), inp)

    # Fully-connected layers
    fc_count = 2
    fc_sizes = [(flat, 2048), (2048, 256)]
    for i in range(0, fc_count):
        inp = fc_bn_dropout(inp, fc_sizes[i], training_name)

    # Last fc layer
    y = layers.Affine((fc_sizes[-1][-1], n_classes), inp)

    loss = layers.SoftmaxLoss(ground_truth, loss_name, y)

    model = net.NeuralNetwork(input_layer, loss, loss_name, ground_truth,
                              training_name, layers.params, layers.grads)

    return model
from net import *
from cs231n.solver import *
import layers

if __name__ == "__main__":
    # Instantiation example
    i1 = layers.Input()
    c1 = layers.Conv((8, 3, 3, 3), {'stride': 1, 'pad': 1}, i1)
    flat = 8 * 28 * 28
    s1 = layers.Reshape((flat, ), c1)
    a1 = layers.Affine((flat, 10), s1)
    l1 = layers.SoftmaxLoss('y', 'loss', a1)
    try:
        layers.load_network('network')
    except IOError:
        pass

    model = NeuralNetwork(i1, l1, 'loss', layers.params, layers.grads)

    data = {
        'X_train': np.ones((2**10, 3, 28, 28)) * 0.1,
        'y_train': np.ones(2**10, dtype=np.int) * 2,
        'X_val': np.ones((2**3, 3, 28, 28)) * 0.1,
        'y_val': np.ones(2**3, dtype=np.int) * 2
    }
    solver = Solver(model,
                    data,
                    update_rule='sgd',
                    optim_config={
                        'learning_rate': 1e-3,
                    },