Example #1
0
  def test_all(self, n):
    _dbn=DBN([784,1000,500,250,30],learning_rate=0.01,cd_k=1)
    _dbn.pretrain(mnist.train.images,128,50)

    _nnet = NN([784, 1000, 500, 250, 30, 250, 500, 1000, 784], 0.01, 128, 50)
    _nnet.load_from_dbn_to_reconstructNN(_dbn)
    _nnet.train(mnist.train.images, mnist.train.images)
    _nnet.test_linear(mnist.test.images, mnist.test.images)

    x_in = mnist.test.images[:30]
    _predict = _nnet.predict(x_in)
    _predict_img = np.concatenate(np.reshape(_predict, [-1, 28, 28]), axis=1)
    x_in = np.concatenate(np.reshape(x_in, [-1, 28, 28]), axis=1)
    img = Image.fromarray(
        (1.0-np.concatenate((_predict_img, x_in), axis=0))*255.0)
    img = img.convert('L')
    img.save(str(n)+'_.jpg')
    img2 = Image.fromarray(
        (np.concatenate((_predict_img, x_in), axis=0))*255.0)
    img2 = img2.convert('L')
    img2.save(str(n)+'.jpg')

    nnet_encoder=NN()
    nnet_encoder.load_layers_from_NN(_nnet,0,4)
    # featrue=nnet_encoder.predict(mnist.test.images)
    nnet_decoder=NN()
    nnet_decoder.load_layers_from_NN(_nnet,5,8)
Example #2
0
    def test_another_rbmtrain(self, n):
        _dbn = DBN([784, 1000, 500, 250, 30], learning_rate=0.01, cd_k=1)
        print(len(mnist.train.images))
        for j in range(5):
            for i in range(10):
                _dbn.pretrain(mnist.train.images[i * 5500:i * 5500 + 5500],
                              128, 5)

        _nnet = NN([784, 1000, 500, 250, 30, 250, 500, 1000, 784], 0.01, 128,
                   50)
        _nnet.load_from_dbn_to_reconstructNN(_dbn)
        _nnet.train(mnist.train.images, mnist.train.images)
        _nnet.test_linear(mnist.test.images, mnist.test.images)

        x_in = mnist.test.images[:30]
        _predict = _nnet.predict(x_in)
        _predict_img = np.concatenate(np.reshape(_predict, [-1, 28, 28]),
                                      axis=1)
        x_in = np.concatenate(np.reshape(x_in, [-1, 28, 28]), axis=1)
        img = Image.fromarray((1.0 - np.concatenate(
            (_predict_img, x_in), axis=0)) * 255.0)
        img = img.convert('L')
        img.save(str(n) + '_.jpg')
        img2 = Image.fromarray((np.concatenate(
            (_predict_img, x_in), axis=0)) * 255.0)
        img2 = img2.convert('L')
        img2.save(str(n) + '.jpg')