├── README.md ├── input_data.py ├── tests_trained.py └── txt └── ytest.txt /README.md: -------------------------------------------------------------------------------- 1 | # IsingGaugeTensorFlow 2 | Toy model that distinguishes ground states from high-temperature states of the 2d Ising lattice Gauge theory. Based on a convolutional neural net implemented in TensorFlow. 3 | 4 | -------------------------------------------------------------------------------- /input_data.py: -------------------------------------------------------------------------------- 1 | """Functions for downloading and reading MNIST data.""" 2 | import gzip 3 | import os 4 | import urllib 5 | import numpy 6 | SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/' 7 | 8 | 9 | def maybe_download(filename, work_directory): 10 | filepath = os.path.join(work_directory, filename) 11 | return filepath 12 | 13 | 14 | def _read32(bytestream): 15 | dt = numpy.dtype(numpy.uint32).newbyteorder('>') 16 | return numpy.frombuffer(bytestream.read(4), dtype=dt) 17 | 18 | 19 | def extract_images(filename,lx): 20 | """Extract the images into a 4D uint8 numpy array [index, y, x, depth].""" 21 | print 'Extracting', filename,'aaaaaa' 22 | 23 | #with gzip.open(filename) as bytestream: 24 | # magic = _read32(bytestream) 25 | # if magic != 2051: 26 | # raise ValueError( 27 | # 'Invalid magic number %d in MNIST image file: %s' % 28 | # (magic, filename)) 29 | # num_images = _read32(bytestream) 30 | # rows = _read32(bytestream) 31 | # cols = _read32(bytestream) 32 | # buf = bytestream.read(rows * cols * num_images) 33 | # data = numpy.frombuffer(buf, dtype=numpy.uint8) 34 | # data = data.reshape(num_images, rows, cols, 1) 35 | data=numpy.loadtxt(filename) 36 | dim=data.shape[0] 37 | data=data.reshape(dim,lx,lx,2) # the two comes from the 2 site unite cell of the toric code. 38 | print data.shape 39 | return data 40 | 41 | 42 | def dense_to_one_hot(labels_dense, num_classes=10): 43 | """Convert class labels from scalars to one-hot vectors.""" 44 | num_labels = labels_dense.shape[0] 45 | index_offset = numpy.arange(num_labels) * num_classes 46 | labels_one_hot = numpy.zeros((num_labels, num_classes)) 47 | labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1 48 | return labels_one_hot 49 | 50 | 51 | def extract_labels(nlabels,filename, one_hot=False): 52 | """Extract the labels into a 1D uint8 numpy array [index].""" 53 | print 'Extracting', filename,'bbbccicicicicib' 54 | 55 | labels=numpy.loadtxt(filename,dtype='uint8') 56 | 57 | if one_hot: 58 | print "LABELS ONE HOT" 59 | print labels.shape 60 | XXX=dense_to_one_hot(labels,nlabels) 61 | print XXX.shape 62 | return dense_to_one_hot(labels,nlabels) 63 | print "LABELS" 64 | print labels.shape 65 | return labels 66 | 67 | 68 | class DataSet(object): 69 | def __init__(self, images, labels, fake_data=False): 70 | if fake_data: 71 | self._num_examples = 10000 72 | else: 73 | assert images.shape[0] == labels.shape[0], ( 74 | "images.shape: %s labels.shape: %s" % (images.shape, 75 | labels.shape)) 76 | self._num_examples = images.shape[0] 77 | # Convert shape from [num examples, rows, columns, depth] 78 | # to [num examples, rows*columns] (assuming depth == 1) 79 | assert images.shape[3] == 2 # the 2 comes from the toric code unit cell 80 | images = images.reshape(images.shape[0], 81 | images.shape[1] * images.shape[2]*2) #the 2 comes from the toric code unit cell 82 | # Convert from [0, 255] -> [0.0, 1.0]. 83 | images = images.astype(numpy.float32) 84 | # images = numpy.multiply(images, 1.0 / 255.0) # commented since it is ising variables 85 | images = numpy.multiply(images, 1.0 ) # multiply by one, instead 86 | self._images = images 87 | self._labels = labels 88 | self._epochs_completed = 0 89 | self._index_in_epoch = 0 90 | 91 | @property 92 | def images(self): 93 | return self._images 94 | 95 | @property 96 | def labels(self): 97 | return self._labels 98 | 99 | @property 100 | def num_examples(self): 101 | return self._num_examples 102 | 103 | @property 104 | def epochs_completed(self): 105 | return self._epochs_completed 106 | 107 | def next_batch(self, batch_size, fake_data=False): 108 | """Return the next `batch_size` examples from this data set.""" 109 | if fake_data: 110 | fake_image = [1.0 for _ in xrange(784)] 111 | fake_label = 0 112 | return [fake_image for _ in xrange(batch_size)], [ 113 | fake_label for _ in xrange(batch_size)] 114 | start = self._index_in_epoch 115 | self._index_in_epoch += batch_size 116 | if self._index_in_epoch > self._num_examples: 117 | # Finished epoch 118 | self._epochs_completed += 1 119 | # Shuffle the data 120 | perm = numpy.arange(self._num_examples) 121 | numpy.random.shuffle(perm) 122 | self._images = self._images[perm] 123 | self._labels = self._labels[perm] 124 | # Start next epoch 125 | start = 0 126 | self._index_in_epoch = batch_size 127 | assert batch_size <= self._num_examples 128 | end = self._index_in_epoch 129 | return self._images[start:end], self._labels[start:end] 130 | 131 | 132 | def read_data_sets(nlabels,lx, train_dir, fake_data=False, one_hot=False ): 133 | class DataSets(object): 134 | pass 135 | data_sets = DataSets() 136 | if fake_data: 137 | data_sets.train = DataSet([], [], fake_data=True) 138 | data_sets.validation = DataSet([], [], fake_data=True) 139 | data_sets.test = DataSet([], [], fake_data=True) 140 | return data_sets 141 | TRAIN_IMAGES = 'Xtrain.txt' 142 | TRAIN_LABELS = 'ytrain.txt' 143 | TEST_IMAGES = 'Xtest.txt' 144 | TEST_LABELS = 'ytest.txt' 145 | #TEST_IMAGES_Trick = 'XtestTrick.txt' 146 | #TEST_LABELS_Trick = 'ytestTrick.txt' 147 | VALIDATION_SIZE = 0 148 | local_file = maybe_download(TRAIN_IMAGES, train_dir) 149 | train_images = extract_images(local_file,lx) 150 | local_file = maybe_download(TRAIN_LABELS, train_dir) 151 | train_labels = extract_labels(nlabels,local_file, one_hot=one_hot) 152 | local_file = maybe_download(TEST_IMAGES, train_dir) 153 | test_images = extract_images(local_file,lx) 154 | local_file = maybe_download(TEST_LABELS, train_dir) 155 | test_labels = extract_labels(nlabels,local_file, one_hot=one_hot) 156 | 157 | #local_file = maybe_download(TEST_IMAGES_Trick, train_dir) 158 | #test_images_Trick = extract_images(local_file,lx) 159 | #local_file = maybe_download(TEST_LABELS_Trick, train_dir) 160 | #test_labels_Trick = extract_labels(nlabels,local_file, one_hot=one_hot) 161 | 162 | validation_images = train_images[:VALIDATION_SIZE] 163 | validation_labels = train_labels[:VALIDATION_SIZE] 164 | train_images = train_images[VALIDATION_SIZE:] 165 | train_labels = train_labels[VALIDATION_SIZE:] 166 | data_sets.train = DataSet(train_images, train_labels) 167 | data_sets.validation = DataSet(validation_images, validation_labels) 168 | data_sets.test = DataSet(test_images, test_labels) 169 | #data_sets.test_Trick = DataSet(test_images_Trick, test_labels_Trick) 170 | return data_sets 171 | -------------------------------------------------------------------------------- /tests_trained.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import input_data 3 | import sys 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | import matplotlib.cm as cm 7 | 8 | # lattice definitions. Neighbours, plaquettes, and vertices. 9 | def plqu(lx): 10 | k=0 11 | ly=lx 12 | nh=2*lx*ly 13 | neig=np.zeros((lx*ly,4),dtype=np.int) 14 | for j in range(ly): 15 | for i in range(lx): 16 | if i