Skip to content

Commit

Permalink
3d conv
Browse files Browse the repository at this point in the history
  • Loading branch information
carrasqu committed Jul 12, 2016
1 parent 8be5410 commit 7517b45
Show file tree
Hide file tree
Showing 3 changed files with 556 additions and 0 deletions.
190 changes: 190 additions & 0 deletions conv_net_3d/conv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import tensorflow as tf
import input_data
import sys


L=200
lx=4 #=int(raw_input('lx'))
V4d=lx*lx*lx*L # 4d volume

training=5000 #=int(raw_input('training'))
bsize=400 #=int(raw_input('bsize'))

# how does the data look like
Ntemp=41 #int(raw_input('Ntemp')) #20 # number of different temperatures used in the simulation
samples_per_T=500 #int(raw_input('samples_per_T')) #250 # number of samples per temperature value
samples_per_T_test=500 # int(raw_input('samples_per_T')) #250 # number of samples per temperature value


numberlabels=2
mnist = input_data.read_data_sets(numberlabels,lx,L,'txt', one_hot=True)



print "reading sets ok"

#sys.exit("pare aqui")

# defining weighs and initlizatinon
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)

def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

# defining the convolutional and max pool layers
def conv3d(x, W):
return tf.nn.conv3d(x, W, strides=[1, 1, 1, 1, 1], padding='VALID')

# defining the model

x = tf.placeholder("float", shape=[None, (lx)*(lx)*(lx)*L]) # placeholder for the spin configurations
#x = tf.placeholder("float", shape=[None, lx*lx*2]) #with padding and no PBC conv net
y_ = tf.placeholder("float", shape=[None, numberlabels])


#first layer
# convolutional layer # 2x2x2 patch size, 2 channel (2 color), 64 feature maps computed
nmaps1=64
spatial_filter_size=2
W_conv1 = weight_variable([spatial_filter_size, spatial_filter_size, spatial_filter_size,L,nmaps1])
# bias for each of the feature maps
b_conv1 = bias_variable([nmaps1])

# applying a reshape of the data to get the two dimensional structure back
#x_image = tf.reshape(x, [-1,lx,lx,2]) # #with padding and no PBC conv net
x_image = tf.reshape(x, [-1,lx,lx,lx,L]) # with PBC

#We then convolve x_image with the weight tensor, add the bias, apply the ReLU function, and finally max pool.

h_conv1 = tf.nn.relu(conv3d(x_image, W_conv1) + b_conv1)

h_pool1=h_conv1

#In order to build a deep network, we stack several layers of this type. The second layer will have 8 features for each 5x5 patch.

# weights and bias of the fully connected (fc) layer. Ihn this case everything looks one dimensiona because it is fully connected
nmaps2=64

#W_fc1 = weight_variable([(lx/2) * (lx/2) * nmaps1,nmaps2 ]) # with maxpool
W_fc1 = weight_variable([(lx-1) * (lx-1)*(lx-1)*nmaps1,nmaps2 ]) # no maxpool images remain the same size after conv

b_fc1 = bias_variable([nmaps2])

# first we reshape the outcome h_pool2 to a vector
#h_pool1_flat = tf.reshape(h_pool1, [-1, (lx/2)*(lx/2)*nmaps1]) # with maxpool

h_pool1_flat = tf.reshape(h_pool1, [-1, (lx-1)*(lx-1)*(lx-1)*nmaps1]) # no maxpool
# then apply the ReLU with the fully connected weights and biases.
h_fc1 = tf.nn.relu(tf.matmul(h_pool1_flat, W_fc1) + b_fc1)

# Dropout: To reduce overfitting, we will apply dropout before the readout layer. We create a placeholder for the probability that a neuron's output is kept during dropout. This allows us to turn dropout on during training, and turn it off during testing. TensorFlow's tf.nn.dropout op automatically handles scaling neuron outputs in addition to masking them, so dropout just works without any additional scaling.

keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

# readout layer. Finally, we add a softmax layer, just like for the one layer softmax regression above.

# weights and bias
W_fc2 = weight_variable([nmaps2, numberlabels])
b_fc2 = bias_variable([numberlabels])

# apply a softmax layer
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)


#Train and Evaluate the Model
# cost function to minimize

#cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
cross_entropy = -tf.reduce_sum(y_*tf.log(tf.clip_by_value(y_conv,1e-10,1.0)))

train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

sess = tf.Session()
sess.run(tf.initialize_all_variables())

for i in range(training):
batch = mnist.train.next_batch(bsize)
if i%100 == 0:
train_accuracy = sess.run(accuracy,feed_dict={
x:batch[0], y_: batch[1], keep_prob: 1.0})
print "step %d, training accuracy %g"%(i, train_accuracy)
print "test accuracy %g"%sess.run(accuracy, feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
#print "test Trick accuracy %g"%sess.run(accuracy, feed_dict={
#x: mnist.test_Trick.images, y_: mnist.test_Trick.labels, keep_prob: 1.0})
# train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print "test accuracy %g"%sess.run(accuracy, feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})



saver = tf.train.Saver([W_conv1, b_conv1, W_fc1,b_fc1,W_fc2,b_fc2])
save_path = saver.save(sess, "./model.ckpt")
print "Model saved in file: ", save_path

#producing data to get the plots we like

f = open('nnout.dat', 'w')

#output of neural net
ii=0
for i in range(Ntemp):
av=0.0
for j in range(samples_per_T_test):
batch=(mnist.test.images[ii,:].reshape(1,lx*lx*lx*L),mnist.test.labels[ii,:].reshape((1,numberlabels)))
res=sess.run(y_conv,feed_dict={x: batch[0], y_: batch[1],keep_prob: 1.0})
av=av+res
#print ii, res
ii=ii+1
av=av/samples_per_T_test
f.write(str(i)+' '+str(av[0,0])+' '+str(av[0,1])+"\n")
f.close()


f = open('acc.dat', 'w')

# accuracy vs temperature
for ii in range(Ntemp):
batch=(mnist.test.images[ii*samples_per_T_test:ii*samples_per_T_test+samples_per_T_test,:].reshape(samples_per_T_test,L*lx*lx*lx), mnist.test.labels[ii*samples_per_T_test:ii*samples_per_T_test+samples_per_T_test,:].reshape((samples_per_T_test,numberlabels)) )
train_accuracy = sess.run(accuracy,feed_dict={
x:batch[0], y_: batch[1], keep_prob: 1.0})
f.write(str(ii)+' '+str(train_accuracy)+"\n")
f.close()


#producing data to get the plots we like

#f = open('nnoutTrick.dat', 'w')

#output of neural net
#ii=0
#for i in range(Ntemp):
# av=0.0
# for j in range(samples_per_T_test):
# batch=(mnist.test_Trick.images[ii,:].reshape((1,2*lx*lx)),mnist.test_Trick.labels[ii,:].reshape((1,numberlabels)))
# res=sess.run(y_conv,feed_dict={x: batch[0], y_: batch[1],keep_prob: 1.0})
# av=av+res
# #print ii, res
# ii=ii+1
# av=av/samples_per_T_test
# f.write(str(i)+' '+str(av[0,0])+' '+str(av[0,1])+"\n")
#f.close()


#f = open('accTrick.dat', 'w')

# accuracy vs temperature
#for ii in range(Ntemp):
# batch=(mnist.test_Trick.images[ii*samples_per_T_test:ii*samples_per_T_test+samples_per_T_test,:].reshape(samples_per_T_test,2*lx*lx), mnist.test_Trick.labels[ii*samples_per_T_test:ii*samples_per_T_test+samples_per_T_test,:].reshape((samples_per_T_test,numberlabels)) )
# train_accuracy = sess.run(accuracy,feed_dict={
# x:batch[0], y_: batch[1], keep_prob: 1.0})
# f.write(str(ii)+' '+str(train_accuracy)+"\n")
#f.close()

172 changes: 172 additions & 0 deletions conv_net_3d/input_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
"""Functions for downloading and reading MNIST data."""
import gzip
import os
import urllib
import numpy
SOURCE_URL = 'https://yann.lecun.com/exdb/mnist/'


def maybe_download(filename, work_directory):
filepath = os.path.join(work_directory, filename)
return filepath


def _read32(bytestream):
dt = numpy.dtype(numpy.uint32).newbyteorder('>')
return numpy.frombuffer(bytestream.read(4), dtype=dt)


def extract_images(filename,lx,Lt):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
print 'Extracting', filename,'aaaaaa'

#with gzip.open(filename) as bytestream:
# magic = _read32(bytestream)
# if magic != 2051:
# raise ValueError(
# 'Invalid magic number %d in MNIST image file: %s' %
# (magic, filename))
# num_images = _read32(bytestream)
# rows = _read32(bytestream)
# cols = _read32(bytestream)
# buf = bytestream.read(rows * cols * num_images)
# data = numpy.frombuffer(buf, dtype=numpy.uint8)
# data = data.reshape(num_images, rows, cols, 1)
data=numpy.loadtxt(filename)
dim=data.shape[0]
data=data.reshape(dim,Lt,lx,lx,lx) # the two comes from the 2 site unite cell of the toric code.
data=numpy.transpose(data,(0,2,3,4,1))
print data.shape
return data


def dense_to_one_hot(labels_dense, num_classes=10):
"""Convert class labels from scalars to one-hot vectors."""
num_labels = labels_dense.shape[0]
index_offset = numpy.arange(num_labels) * num_classes
labels_one_hot = numpy.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return labels_one_hot


def extract_labels(nlabels,filename, one_hot=False):
"""Extract the labels into a 1D uint8 numpy array [index]."""
print 'Extracting', filename,'bbbccicicicicib'

labels=numpy.loadtxt(filename,dtype='uint8')

if one_hot:
print "LABELS ONE HOT"
print labels.shape
XXX=dense_to_one_hot(labels,nlabels)
print XXX.shape
return dense_to_one_hot(labels,nlabels)
print "LABELS"
print labels.shape
return labels


class DataSet(object):
def __init__(self, images, labels, fake_data=False):
if fake_data:
self._num_examples = 10000
else:
assert images.shape[0] == labels.shape[0], (
"images.shape: %s labels.shape: %s" % (images.shape,
labels.shape))
self._num_examples = images.shape[0]
# Convert shape from [num examples, rows, columns, depth]
# to [num examples, rows*columns] (assuming depth == 1)
#assert images.shape[3] == 1 # the 2 comes from the toric code unit cell
images = images.reshape(images.shape[0],
images.shape[1]*images.shape[2]*images.shape[3]*images.shape[4] ) #
# Convert from [0, 255] -> [0.0, 1.0].
images = images.astype(numpy.float32)
# images = numpy.multiply(images, 1.0 / 255.0) # commented since it is ising variables
images = numpy.multiply(images, 1.0 ) # multiply by one, instead
self._images = images
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0

@property
def images(self):
return self._images

@property
def labels(self):
return self._labels

@property
def num_examples(self):
return self._num_examples

@property
def epochs_completed(self):
return self._epochs_completed

def next_batch(self, batch_size, fake_data=False):
"""Return the next `batch_size` examples from this data set."""
if fake_data:
fake_image = [1.0 for _ in xrange(784)]
fake_label = 0
return [fake_image for _ in xrange(batch_size)], [
fake_label for _ in xrange(batch_size)]
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Finished epoch
self._epochs_completed += 1
# Shuffle the data
perm = numpy.arange(self._num_examples)
numpy.random.shuffle(perm)
self._images = self._images[perm]
self._labels = self._labels[perm]
# Start next epoch
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
return self._images[start:end], self._labels[start:end]


def read_data_sets(nlabels,lx,Lt, train_dir, fake_data=False, one_hot=False ):
class DataSets(object):
pass
data_sets = DataSets()
if fake_data:
data_sets.train = DataSet([], [], fake_data=True)
data_sets.validation = DataSet([], [], fake_data=True)
data_sets.test = DataSet([], [], fake_data=True)
return data_sets
TRAIN_IMAGES = 'Xtrain.txt'
TRAIN_LABELS = 'ytrain.txt'
TEST_IMAGES = 'Xtest.txt'
TEST_LABELS = 'ytest.txt'
#TEST_IMAGES_Trick = 'XtestTrick.txt'
#TEST_LABELS_Trick = 'ytestTrick.txt'
VALIDATION_SIZE = 0
local_file = maybe_download(TRAIN_IMAGES, train_dir)
train_images = extract_images(local_file,lx,Lt)
local_file = maybe_download(TRAIN_LABELS, train_dir)
train_labels = extract_labels(nlabels,local_file, one_hot=one_hot)
local_file = maybe_download(TEST_IMAGES, train_dir)
test_images = extract_images(local_file,lx,Lt)
local_file = maybe_download(TEST_LABELS, train_dir)
test_labels = extract_labels(nlabels,local_file, one_hot=one_hot)

#local_file = maybe_download(TEST_IMAGES_Trick, train_dir)
#test_images_Trick = extract_images(local_file,lx)
#local_file = maybe_download(TEST_LABELS_Trick, train_dir)
#test_labels_Trick = extract_labels(nlabels,local_file, one_hot=one_hot)

validation_images = train_images[:VALIDATION_SIZE]
validation_labels = train_labels[:VALIDATION_SIZE]
train_images = train_images[VALIDATION_SIZE:]
print "bababa", train_images.shape
train_labels = train_labels[VALIDATION_SIZE:]
data_sets.train = DataSet(train_images, train_labels)
data_sets.validation = DataSet(validation_images, validation_labels)
data_sets.test = DataSet(test_images, test_labels)
#data_sets.test_Trick = DataSet(test_images_Trick, test_labels_Trick)
return data_sets
Loading

0 comments on commit 7517b45

Please sign in to comment.