示例#1
0
文件: main.py 项目: huxinran/cnn
def main():
    """
    main func
    """

    text, x, y, char2idx, idx2char = getty()
    T = 100

    config = {
        'dim_hidden': 300,
        'l': T,
        'clip': 5,
        'mu': 0.9,
        'step_size': 0.001
    }

    #np.random.seed(42)
    r = RNN(config)
    r.accept([27])

    ttb = r.sample('f', char2idx, idx2char)
    r.fit(x[:T], y[:T], 100, char2idx, idx2char)
    tta = r.sample('f', char2idx, idx2char)
    print(ttb)
    print(tta)
    print(text[:T])
    return

    (data, label) = cifar()
    N = 10000
    data = np.array(data, dtype=float)[:N, ]
    label = np.array(label)[:N, ]

    data = normalize(data)

    config = {
        'input_shape': [3, 32, 32],
        'mu': 0.9,
        'step_size': 0.000001,
        'step_decay': 0.95
    }

    nn = Net(config)
    conv1 = Conv([3, 3], 6)
    relu1 = Relu()
    conv2 = Conv([3, 3], 32)
    relu2 = Relu()
    pool = MaxPool()
    fc = FC([10])

    nn.add(conv1)
    nn.add(relu1)
    nn.add(pool)
    nn.add(fc)

    print(nn)
    nn.fit(data, label, 200)
示例#2
0
    def test_backward(self):
        l = MaxPool()
        l.accept([2, 4, 4])
        x = np.arange(32).reshape(1, 32)
        y = l.forward(x)

        dy = np.array([[[[1, 1], [1, 1]], [[2, 2], [2, 2]]]])
        dx = l.backward(dy)

        self.assertTrue(
            np.allclose(dx, [[
                0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
                2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2
            ]]))
示例#3
0
import cv2


'''
input_img = np.array([[0,0,0,0,0,0], [0,0,50,0,29,0],
          [0,0,80,31,2,0], [0,33,90,0,75,0],
          [0,0,9,0,95,0], [0,0,0,0,0,0]
          ])
'''
train_images = mnist.train_images()[:1000]
train_labels = mnist.train_labels()[:1000]
test_images = mnist.test_images()[:1000] # test image size: 28 x 28
test_labels = mnist.test_labels()[:1000]

conv = Conv(8)   # 28 x 28 -> 26 x 26 x 8
pool = MaxPool() # 26 x 26 x 8 -> 13 x 13 x 8
softmax = Softmax(13*13*8,10) # 10 nodes for 10 digits 0 -> 9

def forward(images,labels):
    # transform image from [0->255] to [-0.5->0.5]
    out = conv.forward((images / 255)-0.5)
    out = pool.forward(out)
    out = softmax.forward(out)
    
    loss = -np.log(out[labels])
    if np.argmax(out) == labels:
        acc = 1
    else:
        acc = 0
    return out,loss,acc
示例#4
0
 def test_repr(self):
     l = MaxPool()
示例#5
0
 def test_forward(self):
     l = MaxPool()
     l.accept([2, 4, 4])
     x = np.arange(32).reshape(1, 2, 4, 4)
     y = l.forward(x)
     self.assertTrue(np.allclose(y, [5, 7, 13, 15, 21, 23, 29, 31]))
示例#6
0
 def test_accept(self):
     l = MaxPool((2, 2))
     self.assertTrue(l.accept([1, 10, 10]))
     pass
示例#7
0
 def test_init(self):
     l = MaxPool((2, 2))
     pass