Esempio n. 1
0
    if phase == 'train':
        X = mnist_dataset[0][0]
        #labels = mnist_dataset[0][1]
    elif phase == 'valid':
        X = mnist_dataset[1][0]
        #labels = mnist_dataset[1][1]
    elif phase == 'test':
        X = mnist_dataset[2][0]
        #labels = mnist_dataset[2][1]

    H0 = encode_x_to_h0(X)
    H1 = encode_h0_to_h1(H0)

    rH0 = decode_h1_to_h0(H1)
    rX = decode_h0_to_x(rH0)

    quadratic_losses = ((X - rX)**2).sum(axis=1)
    cross_entropy_losses = (X * np.log(rX) +
                            (1 - X) * np.log(1 - rX)).sum(axis=1)

    mean_losses[phase] = {}
    mean_losses[phase]['quadratic_losses'] = quadratic_losses.mean()
    mean_losses[phase]['cross_entropy_losses'] = cross_entropy_losses.mean()

    print "phase %s" % (phase, )
    print "    mean %s : %f" % ('quadratic loss',
                                mean_losses[phase]['quadratic_losses'])
    print "    mean %s : %f" % ('cross-entropy loss',
                                mean_losses[phase]['cross_entropy_losses'])
def main(argv):
    """

    """

    import getopt
    import cPickle

    try:
        #opts, args = getopt.getopt(sys.argv[1:], "hv", ["input_x=", "input_h0=", "input_h1=", "input_rh0=", "output_h0=", "output_h1=", "output_rh0=", "output_rx="])
        opts, args = getopt.getopt(sys.argv[1:], "hv", ["input_h1=", "output_rx=", "output_digits_png="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    verbose = False

    input_h1 = None
    output_rx = None
    output_digits_png = None

    for o, a in opts:
        if o == "-v":
            verbose = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("--input_h1"):
            input_h1 = a
        elif o in ("--output_rx"):
            output_rx = a
        elif o in ("--output_digits_png"):
            output_digits_png = a

    assert os.path.exists(input_h1)
    assert output_rx is not None


    ##### Performing the conversion #####

    H1 = cPickle.load(open(input_h1, "r"))
    print "Read %s" % (input_h1,)
    rH0 = decode_h1_to_h0(H1)
    rX = decode_h0_to_x(rH0)
    cPickle.dump(rX, open(output_rx, "w"))
    print "Wrote %s" % (output_rx,)


    ##### Plotting the digits if desired #####

    if output_digits_png is not None:

        import refactor_gp
        import refactor_gp.yann_dauphin_utils
        from refactor_gp.yann_dauphin_utils import tile_raster_images
        I = tile_raster_images(rX, (28,28), (int(rX.shape[0]/20) + 1, 20))

        import matplotlib
        import pylab
        import matplotlib.pyplot as plt

        from PIL import Image
        im = Image.fromarray(I)
        im.save(output_digits_png)
        #im.save("/u/alaingui/umontreal/denoising_autoencoder/refactor_gp/junk/mnist.png")
        print "Wrote %s" % (output_digits_png,)
    if phase == 'train':
        X = mnist_dataset[0][0]
        #labels = mnist_dataset[0][1]
    elif phase == 'valid':
        X = mnist_dataset[1][0]
        #labels = mnist_dataset[1][1]
    elif phase == 'test':
        X = mnist_dataset[2][0]
        #labels = mnist_dataset[2][1]

    H0 = encode_x_to_h0( X )
    H1 = encode_h0_to_h1( H0 )

    rH0 = decode_h1_to_h0( H1 )
    rX  = decode_h0_to_x( rH0 )

    quadratic_losses = ((X - rX)**2).sum(axis=1)
    cross_entropy_losses = (X * np.log(rX) + (1-X) * np.log(1-rX)).sum(axis=1)

    mean_losses[phase] = {}
    mean_losses[phase]['quadratic_losses'] = quadratic_losses.mean()
    mean_losses[phase]['cross_entropy_losses'] = cross_entropy_losses.mean()

    print "phase %s" % (phase,)
    print "    mean %s : %f" % ('quadratic loss', mean_losses[phase]['quadratic_losses'])
    print "    mean %s : %f" % ('cross-entropy loss', mean_losses[phase]['cross_entropy_losses'])

    cPickle.dump(X, open(os.path.join(output_dir, "yann_%s_X.pkl" % (phase,)), "w"))
    cPickle.dump(H0, open(os.path.join(output_dir, "yann_%s_H0.pkl" % (phase,)), "w"))
    cPickle.dump(H1, open(os.path.join(output_dir, "yann_%s_H1.pkl" % (phase,)), "w"))
def main(argv):
    """

    """

    import getopt
    import cPickle

    try:
        # opts, args = getopt.getopt(sys.argv[1:], "hv", ["input_x=", "input_h0=", "input_h1=", "input_rh0=", "output_h0=", "output_h1=", "output_rh0=", "output_rx="])
        opts, args = getopt.getopt(sys.argv[1:], "hv", ["input_h1=", "output_rx=", "output_digits_png="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    verbose = False

    input_h1 = None
    output_rx = None
    output_digits_png = None

    for o, a in opts:
        if o == "-v":
            verbose = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("--input_h1"):
            input_h1 = a
        elif o in ("--output_rx"):
            output_rx = a
        elif o in ("--output_digits_png"):
            output_digits_png = a

    assert os.path.exists(input_h1)
    assert output_rx is not None

    ##### Performing the conversion #####

    H1 = cPickle.load(open(input_h1, "r"))
    print "Read %s" % (input_h1,)
    rH0 = decode_h1_to_h0(H1)
    rX = decode_h0_to_x(rH0)
    cPickle.dump(rX, open(output_rx, "w"))
    print "Wrote %s" % (output_rx,)

    ##### Plotting the digits if desired #####

    if output_digits_png is not None:

        import refactor_gp
        import refactor_gp.yann_dauphin_utils
        from refactor_gp.yann_dauphin_utils import tile_raster_images

        I = tile_raster_images(rX, (28, 28), (int(rX.shape[0] / 20) + 1, 20))

        import matplotlib
        import pylab
        import matplotlib.pyplot as plt

        from PIL import Image

        im = Image.fromarray(I)
        im.save(output_digits_png)
        # im.save("/u/alaingui/umontreal/denoising_autoencoder/refactor_gp/junk/mnist.png")
        print "Wrote %s" % (output_digits_png,)